C#讀取和寫入文件


一、讀取文件

如果你要讀取的文件內容不是很多,  

  可以使用 File.ReadAllText(FilePath) 或指定編碼方式 File.ReadAllText(FilePath, Encoding)的方法。

  它們都一次將文本內容全部讀完,並返回一個包含全部文本內容的字符串

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

    //也可以指定編碼方式 
    string str2 = File.ReadAllText(@"c:\temp\ascii.txt", Encoding.ASCII);

  也可以使用方法File.ReadAllLines。該方法返回一個字符串數組。每一行都是一個數組元素。

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

    //也可以指定編碼方式 
    string[] strs2 = File.ReadAllLines(@"c:\temp\ascii.txt", Encoding.ASCII);

  當文本的內容比較大時

    我們就不要將文本內容一次讀完,而應該采用流(Stream)的方式來讀取內容。.Net為我們封裝了StreamReader類。

    初始化StreamReader類有很多種方式。

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

 

二、寫取文件

  如果你要寫入的內容不是很多

    可以使用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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM