C#读取和写入文件

1、读取文件数组

若是你要读取的文件内容不是不少,  app

  能够使用 File.ReadAllText(FilePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding)的方法。编码

  它们都一次将文本内容所有读完,并返回一个包含所有文本内容的字符串spa

    string str = File.ReadAllText(@"c:\temp\ascii.txt");

    //也能够指定编码方式 
    string str2 = File.ReadAllText(@"c:\temp\ascii.txt", Encoding.ASCII);rest

  也能够使用方法File.ReadAllLines。该方法返回一个字符串数组。每一行都是一个数组元素。对象

    string[] strs = File.ReadAllLines(@"c:\temp\ascii.txt"); 

    //也能够指定编码方式 
    string[] strs2 = File.ReadAllLines(@"c:\temp\ascii.txt", Encoding.ASCII);utf-8

  当文本的内容比较大时ci

    咱们就不要将文本内容一次读完,而应该采用流(Stream)的方式来读取内容。.Net为咱们封装了StreamReader类。字符串

    初始化StreamReader类有不少种方式。string

      StreamReader sr1 = new StreamReader(@"c:\temp\utf-8.txt"); 
      //一样也能够指定编码方式 
      StreamReader sr2 = new StreamReader(@"c:\temp\utf-8.txt", Encoding.UTF8);

    

      FileStream fs = new FileStream(@"C:\temp\utf-8.txt", FileMode.Open, FileAccess.Read, FileShare.None); 
      StreamReader sr3 = new StreamReader(fs); 
      StreamReader sr4 = new StreamReader(fs, Encoding.UTF8);

      FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt"); 
      // OpenText 建立一个UTF-8 编码的StreamReader对象 
      StreamReader sr5 = myFile.OpenText();

      // OpenText 建立一个UTF-8 编码的StreamReader对象 
      StreamReader sr6 = File.OpenText(@"C:\temp\utf-8.txt");

    初始化完成以后,你能够每次读一行,也能够每次读一个字符 ,还能够每次读几个字符,甚至也能够一次将全部内容读完。

      // 读一行 
      string nextLine = sr.ReadLine();

      // 读一个字符 
      int nextChar = sr.Read();

      // 读100个字符 
      int nChars = 100; 
      char[] charArray = new char[nChars]; 
      int nCharsRead = sr.Read(charArray, 0, nChars);      
      
      // 所有读完 
      string restOfStream = sr.ReadToEnd();

    使用完StreamReader以后,不要忘记关闭它:

      sr.Closee();

 

  假如咱们须要一行一行的读,将整个文本文件读完,下面看一个完整的例子:

    StreamReader sr = File.OpenText(@"C:\temp\ascii.txt"); 
    string nextLine; 
    while ((nextLine = sr.ReadLine()) != null) 
    { 
        Console.WriteLine(nextLine); 
    } 
    sr.Close();

 

2、写取文件

  若是你要写入的内容不是不少

    能够使用File.WriteAllText方法来一次将内容所有写如文件。

    若是你要将一个字符串的内容写入文件,能够用File.WriteAllText(FilePath) 或指定编码方式 File.WriteAllText(FilePath, Encoding)方法。

      string str1 = "Good Morning!";

      File.WriteAllText(@"c:\temp\test\ascii.txt", str1);

      // 也能够指定编码方式

      File.WriteAllText(@"c:\temp\test\ascii-2.txt", str1, Encoding.ASCII);

    若是你有一个字符串数组,你要将每一个字符串元素都写入文件中,能够用File.WriteAllLines方法:

      string[] strs = { "Good Morning!", "Good Afternoon!" };

      File.WriteAllLines(@"c:\temp\ascii.txt", strs);

      // 也能够指定编码方式

 

      File.WriteAllLines(@"c:\temp\ascii-2.txt", strs, Encoding.ASCII);

    使用File.WriteAllText或File.WriteAllLines方法时,若是指定的文件路径不存在,会建立一个新文件;若是文件已经存在,则会覆盖原文件。

  当要写入的内容比较多时

    一样也要使用流(Stream)的方式写入。.Net封装的类是StreamWriter。

    初始化StreamWriter类一样有不少方式:

      // 若是文件不存在,建立文件; 若是存在,覆盖文件 
      StreamWriter sw1 = new StreamWriter(@"c:\temp\utf-8.txt"); 

      // 也能够指定编码方式 
      // true 是 append text, false 为覆盖原文件 
      StreamWriter sw2 = new StreamWriter(@"c:\temp\utf-8.txt", true, Encoding.UTF8);

      // FileMode.CreateNew: 若是文件不存在,建立文件;若是文件已经存在,抛出异常 
      FileStream fs = new FileStream(@"C:\temp\utf-8.txt", FileMode.CreateNew, FileAccess.Write, FileShare.Read); 
      // UTF-8 为默认编码 
      StreamWriter sw3 = new StreamWriter(fs); 
      StreamWriter sw4 = new StreamWriter(fs, Encoding.UTF8);

      // 若是文件不存在,建立文件; 若是存在,覆盖文件 
      FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt"); 
      StreamWriter sw5 = myFile.CreateText();

    初始化完成后,能够用StreamWriter对象一次写入一行,一个字符,一个字符数组,甚至一个字符数组的一部分。

      // 写一个字符            
      sw.Write('a');

      // 写一个字符数组 
      char[] charArray = new char[100]; 
      // initialize these characters 
      sw.Write(charArray);

      // 写一个字符数组的一部分 
      sw.Write(charArray, 10, 15);

    一样,StreamWriter对象使用完后,不要忘记关闭。

      sw.Close();

 

    最后来看一个完整的使用StreamWriter一次写入一行的例子:

      FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt"); 
      StreamWriter sw = myFile.CreateText();

      string[] strs = { "早上好", "下午好" };            
      foreach (var s in strs) 
      { 
          sw.WriteLine(s); 
      } 
      sw.Close();

相关文章
相关标签/搜索