(一) 读取文件
如果你要读取的文件内容不是很多,可以使用 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);
string[] strs = File.ReadAllLines(@"c:\temp\ascii.txt");
// 指定编码方式
string[] strs2 = File.ReadAllLines(@"c:\temp\ascii.txt", Encoding.ASCII);
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 = 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);
string[] strs = { "Good Morning!", "Good Afternoon!" };
File.WriteAllLines(@"c:\temp\ascii.txt", strs);
File.WriteAllLines(@"c:\temp\ascii-2.txt", strs, Encoding.ASCII);
当要写入的内容比较多时,同样也要使用流(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();
// 写一个字符
sw.Write('a');// 写一个字符数组
char[] charArray = new char[100];
// initialize these characters
sw.Write(charArray);
// 写一个字符数组的一部分
sw.Write(charArray, 10, 15);
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();
本文介绍了使用C#进行文件读写的多种方法,包括利用File类的ReadAllText和WriteAllText方法快速读写小文件,以及通过StreamReader和StreamWriter类处理大文件。详细解释了如何逐行读取文件和逐行写入内容。
2477

被折叠的 条评论
为什么被折叠?



