讀取文件
如果要讀取的文件內容不是不是很多,可以選擇File.ReadAllText將文本內容一次讀完,並返回全部文本內容的字符串。
1 string str = File.ReadAllText(@"c:\temp\ascii.txt"); //一次性讀取文本內容 2 string str2 = File.ReadAllText(@"c:\temp\ascii.txt", Encoding.ASCII); // 也可以指定編碼方式
也可以使用File.ReadAllLines返回字符串數組。每一行是一個數組元素。
1 string[] strs = File.ReadAllLines(@"c:\temp\ascii.txt"); 2 // 也可以指定編碼方式 3 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");
初始化完成后,可以開始讀取字符串。讀取字符串方式包括。
1 // 讀一行 2 string nextLine = sr.ReadLine(); 3 // 讀一個字符 4 int nextChar = sr.Read(); 5 // 讀100個字符 6 int nChars = 100; 7 char[] charArray = new char[nChars]; 8 int nCharsRead = sr.Read(charArray, 0, nChars); 9 // 全部讀完 10 string restOfStream = sr.ReadToEnd();
使用StreamReader后要記得關閉。
1 sr.Closee();
以下Demo一行一行讀取,將整個文本讀完。
1 StreamReader sr = File.OpenText(@"C:\temp\ascii.txt"); 2 string nextLine; 3 while ((nextLine = sr.ReadLine()) != null) 4 { 5 Console.WriteLine(nextLine); 6 } 7 sr.Close();
寫入文件
如果寫入的內容不是很多,可以采用File.WriteAllText一次性寫入。
1 //創建或覆蓋文件 2 string str1 = "Good Morning!"; File.WriteAllText(@"c:\temp\test\ascii.txt", str1); 3 // 也可以指定編碼方式 4 File.WriteAllText(@"c:\temp\test\ascii-2.txt", str1, Encoding.ASCII);
如果有一個字符串數組,要將每一個字符串元素寫入文件中,可以用File.WriteAllLines的方法。
1 //創建或覆蓋文件 2 string[] strs = { "Good Morning!", "Good Afternoon!" }; 3 //寫入字符串 4 File.WriteAllLines(@"c:\temp\ascii.txt", strs); 5 //固定編碼格式寫入字符串 6 File.WriteAllLines(@"c:\temp\ascii-2.txt", strs, Encoding.ASCII);
使用File.WriteAllText或者File.WriteAllLines方法時,如果指定文件路徑不存在,會創建一個新文件;如果文件已存在,則會覆蓋原文件。
當寫入的文本內容較多時,同樣使用流(Stream)的方式寫入,.Net封裝類是StreamWriter。初始StreamWriter方式如下:
1 // 如果文件不存在,創建文件; 如果存在,覆蓋文件 2 StreamWriter sw1 = new StreamWriter(@"c:\temp\utf-8.txt"); 3 4 // 也可以指定編碼方式 5 // true 是 append text, false 為覆蓋原文件 6 StreamWriter sw2 = new StreamWriter(@"c:\temp\utf-8.txt", true, Encoding.UTF8); 7 8 // FileMode.CreateNew: 如果文件不存在,創建文件;如果文件已經存在,拋出異常 9 FileStream fs = new FileStream(@"C:\temp\utf-8.txt", FileMode.CreateNew, FileAccess.Write, FileShare.Read); 10 // UTF-8 為默認編碼 11 StreamWriter sw3 = new StreamWriter(fs); 12 StreamWriter sw4 = new StreamWriter(fs, Encoding.UTF8); 13 14 // 如果文件不存在,創建文件; 如果存在,覆蓋文件 15 FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt"); 16 StreamWriter sw5 = myFile.CreateText();
初始化完成后,可以用StreamWriter進行寫入。
1 // 寫一個字符 2 sw.Write('a'); 3 4 // 寫一個字符數組 5 char[] charArray = new char[100]; 6 // initialize these characters 7 sw.Write(charArray); 8 // 寫一個字符數組的一部分 9 sw.Write(charArray, 10, 15);
使用完成后,需要關閉。
1 sw.Close();
完整使用StreamWriter一次寫入一行數據。
1 FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt"); 2 StreamWriter sw = myFile.CreateText(); 3 string[] strs = { "早上好", "下午好" }; 4 foreach (var s in strs) 5 { 6 sw.WriteLine(s); 7 } 8 sw.Close();
參考網址
[1] https://www.cnblogs.com/rainbow70626/p/10646981.html