zoukankan      html  css  js  c++  java
  • 使用StreamReader和StreamWriter读写文本文件

    1.StreamReader---简化了文本类型的流的读取

    ReadToEnd用于从当前位置一直读到最后,内容大的话会占内存。相当于File.ReadAllText。

    ReadLine读取一行,如果到了末尾,则返回null。

     1 //用来逐行读取文本文件
     2 using (StreamReader reader = new StreamReader("1.txt", Encoding.Default))
     3 {
     4   ////StreamReader是逐行读取文本文件
     5   ////直到文件的末尾
     6   //while (!reader.EndofStream)
     7   //{
     8   //  Console.WriteLine(reader.ReadLine());
     9   //}
    10 
    11   string line = null;
    12   while ((line = reader.ReadLine()) != null)
    13   {
    14     Console.WriteLine(line);
    15   }
    16   Console.ReadKey();
    17 }

    2.StreamWriter

     1 using (StreamWriter writer = new StreamWriter("test.txt", true, Encoding.Default))
     2 {
     3   for (int i = 0; i<100: i++)
     4   {
     5     writer.WriteLine(i);
     6   }
     7 }
     8 
     9 Console.WriteLine("ok");
    10 Console.ReadKey();
  • 相关阅读:
    windows red5相关
    redis集群及相关的使用
    c# 并行运算
    C# Thread
    html5网页录音
    netcore log4相关
    Swagger插件netcore配置
    MongoDB操作集
    .Net Core知识点
    C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
  • 原文地址:https://www.cnblogs.com/-lee-/p/8086715.html
Copyright © 2011-2022 走看看