C#程序可一次将文件内容读入字符串
使用ReadToEnd()方法读取字符串中文件的内容。
在StreamReader下设置它并读取文件-
using (StreamReader sr = new StreamReader("new.txt")){
string res = sr.ReadToEnd();
Console.WriteLine(res);
}以下是完整的代码-
示例
using System.IO;
using System;
public class Demo {
public static void Main() {
using (StreamWriter sw = new StreamWriter("new.txt")) {
sw.WriteLine("One");
sw.WriteLine("Two");
}
using (StreamReader sr = new StreamReader("new.txt")) {
string res = sr.ReadToEnd();
Console.WriteLine(res);
}
}
}它创建文件“new.text”并向其中添加文本。之后,使用StreamReader类和ReadToEnd()方法,它将文件的内容读取为字符串-
输出结果
以下是输出。
One Two
热门推荐
10 女生高级生日祝福语简短
11 别人复读怎么祝福语简短
12 赠男友祝福语大全简短
13 鱼的内涵简短祝福语
14 祝女人美丽祝福语简短
15 中考祝福语简短高级句
16 韩语贺卡祝福语简短版
17 好友上岸成功祝福语简短
18 成年女性生日祝福语简短