zoukankan      html  css  js  c++  java
  • 写入文件总结

    方法二:
    FileStream fs
    =new FileStream(sf.FileName,FileMode.Create);

    StreamWriter sw
    =new StreamWriter(fs);
    sw.Write(textBox1.Text);
    sw.Flush();
    sw.Close();
    fs.Close();

    方法一:
    //实例化一个文件流
    FileStream fs=new FileStream(sf.FileName,FileMode.Create);
    //获得字节数组
    byte[] date=new UTF8Encoding().GetBytes(this.textBox1.Text);
    //开始写入
    fs.Write(date,0,date.Length);
    //清空缓冲区,关闭流
    fs.Flush();
    fs.Close();
     
    方法三:
    FileStream fs
    =new FileStream(sf.FileName,FileMode.Create);
    BinaryWrite bw
    =new BinaryWrite(fs);
    bw.Write(textBox1.Text);
    bw.Flush();
    bw.Close();
    fs.Close();




    //备注:其中fs为SaveFileDialog的实例
     
     
     
     ----2012-3-21
    今天导师交给我的任务就是遍历数据 然后筛选每一行的第三个数据不是65535的,下面是我实现的代码:
    private readonly static char[] split = new char[] { ',' };
    
            static void File()
            {
                try
                {
                    using (StreamReader streamReader = new StreamReader(@"E:\result\result.csv", System.Text.Encoding.UTF8))
                    {
                        string line;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            //判断第三个数据是否为65535
                            string[] str = line.Split(split);
                            //第三个数据不为65535 则把数据存储起来
                            if (!str[2].Equals("65535"))
                            {
                                try
                                {
                                    using (StreamWriter streamWrite = new StreamWriter(@"E:\result\resultNew.csv", true, System.Text.Encoding.UTF8))
                                    {
                                        streamWrite.WriteLine("{0},{1},{2}", str[0], str[1], str[2]);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                            else
                                Console.WriteLine("****{0},{1},{2}", str[0], str[1], str[2]);
                            System.Console.WriteLine(line);
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex);
                }
    
    
    
                //System.Console.WriteLine(line);
            }
            static void Main(string[] args)
            {
                File();
            }
    

      

     
     
     
     
     
     
  • 相关阅读:
    文件操作
    需特别注意的地方(关于内存机制)
    数据类型的汇总和功能
    python之http请求及响应
    8.centos7进入单用户
    Android Studio使用总结
    django之数据库models
    django之错题集
    python之mysql安装配置
    python之pycharm的debug调试使用
  • 原文地址:https://www.cnblogs.com/huaizuo/p/2080860.html
Copyright © 2011-2022 走看看