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();
            }
    

      

     
     
     
     
     
     
  • 相关阅读:
    Linux RAID部署
    系统运维架构师体系
    Linux系统上文件的特殊权限及文件acl
    Linux磁盘使用及文件系统管理
    中小规模网站架构组成
    优化配置模板主机
    网络原理基础
    MySQL二进制安装
    网络通讯基础
    点击改变背景
  • 原文地址:https://www.cnblogs.com/huaizuo/p/2080860.html
Copyright © 2011-2022 走看看