C#StreamReader类用于从流中读取字符串。它继承TextReader类。它提供read()和ReadLine()方法从流中读取数据。
让无涯教程看一下从文件中读取单行数据的StreamReader类的简单示例。
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-streamreader.html
来源:LearnFk无涯教程网
using System; using System.IO; public class StreamReaderExample { public static void Main(string[] args) { FileStream f = new FileStream("e:\\output.txt", FileMode.OpenOrCreate); StreamReader s = new StreamReader(f); string line=s.ReadLine(); Console.WriteLine(line); s.Close(); f.Close(); } }
输出:
Hello C#
using System; using System.IO; public class StreamReaderExample { public static void Main(string[] args) { FileStream f = new FileStream("e:\\a.txt", FileMode.OpenOrCreate); StreamReader s = new StreamReader(f); string line = ""; while ((line = s.ReadLine()) != null) { Console.WriteLine(line); } s.Close(); f.Close(); } }
输出:
Hello C# this is file handling
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)