C# 文本文件的读写

读取文件

  如果要读取的文件内容不是不是很多,可以选择File.ReadAllText将文本内容一次读完,并返回全部文本内容的字符串。

1 string str = File.ReadAllText(@"c:	empascii.txt");   //一次性读取文本内容
2 string str2 = File.ReadAllText(@"c:	empascii.txt", Encoding.ASCII);    // 也可以指定编码方式

   也可以使用File.ReadAllLines返回字符串数组。每一行是一个数组元素。

1 string[] strs = File.ReadAllLines(@"c:	empascii.txt"); 
2 // 也可以指定编码方式 
3 string[] strs2 = File.ReadAllLines(@"c:	empascii.txt", Encoding.ASCII);

   当文本内容较多的时候,不适合将文本内容一次性读完,而应该采取流(Stream)的方式,.Net中为我们封装了StreamReader类。初始化StreamReader类的方式如下:

StreamReader sr1 = new StreamReader(@"c:	emputf-8.txt"); 
// 同样也可以指定编码方式 
StreamReader sr2 = new StreamReader(@"c:	emputf-8.txt", Encoding.UTF8);

FileStream fs = new FileStream(@"C:	emputf-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:	emputf-8.txt"); 
// OpenText 创建一个UTF-8 编码的StreamReader对象 

StreamReader sr5 = myFile.OpenText();
// OpenText 创建一个UTF-8 编码的StreamReader对象 
StreamReader sr6 = File.OpenText(@"C:	emputf-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:	empascii.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:	emp	estascii.txt", str1); 
3 // 也可以指定编码方式 
4 File.WriteAllText(@"c:	emp	estascii-2.txt", str1, Encoding.ASCII);

   如果有一个字符串数组,要将每一个字符串元素写入文件中,可以用File.WriteAllLines的方法。

1 //创建或覆盖文件
2 string[] strs = { "Good Morning!", "Good Afternoon!" }; 
3 //写入字符串
4 File.WriteAllLines(@"c:	empascii.txt", strs); 
5 //固定编码格式写入字符串
6 File.WriteAllLines(@"c:	empascii-2.txt", strs, Encoding.ASCII);

   使用File.WriteAllText或者File.WriteAllLines方法时,如果指定文件路径不存在,会创建一个新文件;如果文件已存在,则会覆盖原文件。

  当写入的文本内容较多时,同样使用流(Stream)的方式写入,.Net封装类是StreamWriter。初始StreamWriter方式如下:

 1 // 如果文件不存在,创建文件; 如果存在,覆盖文件 
 2 StreamWriter sw1 = new StreamWriter(@"c:	emputf-8.txt"); 
 3 
 4 // 也可以指定编码方式 
 5 // true 是 append text, false 为覆盖原文件 
 6 StreamWriter sw2 = new StreamWriter(@"c:	emputf-8.txt", true, Encoding.UTF8);
 7 
 8 // FileMode.CreateNew: 如果文件不存在,创建文件;如果文件已经存在,抛出异常 
 9 FileStream fs = new FileStream(@"C:	emputf-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:	emputf-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:	emputf-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

原文地址:https://www.cnblogs.com/luyj00436/p/11654272.html