帝国CMS模板大全
www.admin99.cn
www.92cms.cn 帝国CMS模板下载站!,情怀,养站,二次开发!源码需求比较大的一站式会员下载,价更省!!!

C#读写文本文件(.txt)的方法实例

读取txt文件

如果你要读取的文件内容不是很多,可以使用 File.ReadAllText(filePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding)的方法。它们都一次性将文本内容全部读完,并返回一个包含全部文本内容的字符串

string str1 = File.ReadAllText(@”c:\temp\a.txt”);
//也可以指定编码方式
string str2 = File.ReadAllText(@”c:\temp\a.txt”, Encoding.ASCII);

也可以使用方法File.ReadAllLines,该方法一次性读取文本内容的所有行,返回一个字符串数组,数组元素是每一行的内容

string[] strs1 = File.ReadAllLines(@”c:\temp\a.txt”);
// 也可以指定编码方式
string[] strs2 = File.ReadAllLines(@”c:\temp\a.txt”, Encoding.ASCII);

当文本的内容比较大时,我们就不要将文本内容一次性读完,而应该采用流(Stream)的方式来读取内容。

.Net为我们封装了StreamReader类,它旨在以一种特定的编码从字节流中读取字符。StreamReader类的方法不是静态方法,所以要使用该类读取文件首先要实例化该类,在实例化时,要提供读取文件的路径。实例化StreamReader类有很多种方式。下面我罗列出几种:

StreamReader sR1 = new StreamReader(@”c:\temp\a.txt”);
// 同样也可以指定编码方式
StreamReader sR2 = new StreamReader(@”c:\temp\a.txt”, Encoding.UTF8);

FileStream fS = new FileStream(@”C:\temp\a.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\a.txt”);
// OpenText 创建一个UTF-8 编码的StreamReader对象
StreamReader sR5 = myFile.OpenText();
// OpenText 创建一个UTF-8 编码的StreamReader对象
StreamReader sR6 = File.OpenText(@”C:\temp\a.txt”);

初始化完成之后,你可以每次读一行,也可以每次读一个字符 ,还可以每次读几个字符,甚至也可以一次将所有内容全部读完

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

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

// 读100个字符
int n = 100;
char[] charArray = new char[n];
int nCharsRead = sR.Read(charArray, 0, n);

// 全部读完
string restOfStream = sR.ReadToEnd();

使用完StreamReader之后,不要忘记关闭它: sR.Close();

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

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

写入txt文件

写文件和读文件一样,如果你要写入的内容不是很多,可以使用File.WriteAllText方法来一次将内容全部写如文件。如果你要将一个字符串的内容写入文件,可以用File.WriteAllText(FilePath) 或指定编码方式 File.WriteAllText(FilePath, Encoding)方法

string str1 = “Good Morning!”;
File.WriteAllText(@”c:\temp\test\a.txt”, str1);
// 也可以指定编码方式
File.WriteAllText(@”c:\temp\test\a.txt”, str1, Encoding.ASCII);

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

string[] strs = { “Good Morning!”,”Good Afternoon!”,”Good Evening!”};
File.WriteAllLines(@”c:\temp\a.txt”, strs);
// 也可以指定编码方式
File.WriteAllLines(@”c:\temp\a.txt”, strs, Encoding.ASCII);

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

当要写入的内容比较多时,同样也要使用流(Stream)的方式写入

.Net为我们封装了StreamWriter类,它以一种特定的编码向字节流中写入字符。StreamWriter类的方法同样也不是静态方法,所以要使用该类写入文件首先要实例化该类,实例化StreamWriter类同样有很多方式:

// 如果文件不存在,创建文件; 如果存在,覆盖文件
StreamWriter sW1 = new StreamWriter(@”c:\temp\a.txt”);

// 也可以指定编码方式, true 是 Appendtext, false 为覆盖原文件
StreamWriter sW2 = new StreamWriter(@”c:\temp\a.txt”, true, Encoding.UTF8);

// FileMode.CreateNew: 如果文件不存在,创建文件;如果文件已经存在,抛出异常
FileStream fS = new FileStream(@”C:\temp\a.txt”, FileMode.CreateNew, FileAccess.Write, FileShare.Read);
StreamWriter sW3 = new StreamWriter(fS);
StreamWriter sW4 = new StreamWriter(fS, Encoding.UTF8);

// 如果文件不存在,创建文件; 如果存在,覆盖文件
FileInfo myFile = new FileInfo(@”C:\temp\a.txt”);
StreamWriter sW5 = myFile.CreateText();

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

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

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

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

同样,StreamWriter对象使用完后,不要忘记关闭。sW.Close(); 最后来看一个完整的使用StreamWriter一次写入一行的例子:

FileInfo myFile = new FileInfo(@”C:\temp\a.txt”);
StreamWriter sW = myFile.CreateText();
string[] strs = { “早上好”, “下午好” ,”晚上好};
foreach (var s in strs)
{
sW.WriteLine(s);
}
sW.Close();

总结

到此这篇关于C#读写文本文件(.txt)的文章就介绍到这了,更多相关C#读写txt文本文件内容请搜索www.admin99.cn以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.admin99.cn!

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《C#读写文本文件(.txt)的方法实例》
文章链接:https://www.admin99.cn/7459
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
QQ站长交流群:953950264

登录

找回密码

注册