zoukankan      html  css  js  c++  java
  • C#中的IO流操作(FileStream)

    StreamReader和StreamWriter适用于对文本文件的操作,因为它是以字符为单位进行的操作

    不用担心编码问题

                using (Stream s = new FileStream(@"C:UsersqqDesktop新建文件夹HTMLqq.txt", FileMode.Open, FileAccess.Read))
                using (StreamReader r = new StreamReader(s, Encoding.Default))  //读取流中的文本
                {
                    string str;
                    while ((str = r.ReadLine()) != null) //每次读取一行,当读取的内容为null是,读取完成
                    {
                        Console.WriteLine(str);
                    }
                }
    

      

    FileStream 
    他是以字节对文件的读写操作的
        using (Stream s = new FileStream(@"C:UsersqqDesktop新建文件夹HTMLqq.txt", FileMode.Open))
                using (Stream w = new FileStream(@"C:UsersqqDesktop新建文件夹HTMLyzk.txt", FileMode.Create))
                {
                   // s.CopyTo(w); //复制文件
    
                    byte[] b = new byte[10];
    
                    int len = 0;
                    while ((len = s.Read(b, 0, b.Length)) > 0)  //每次读取的数据放到b数组中
                    {
                        //Console.WriteLine(Encoding.Default.GetString(b,0,b.Length));
                        w.Write(b, 0, b.Length); //把数组中的数据写入新的文件中
                    }
                }
  • 相关阅读:
    YII框架学习(二)
    YII框架学习(一)
    valid number 判断字符串是否为有效数字
    leetcode Add Binary
    leetcode Minimum Path Sum
    leetcode Unique Paths II
    leetcode[61] Unique Paths
    leetcode[60] Rotate List
    leetcode Permutation Sequence
    leetcode Spiral Matrix II
  • 原文地址:https://www.cnblogs.com/ZX-LMY/p/5825786.html
Copyright © 2011-2022 走看看