//文件流
using (var streamReader = new StreamReader("text2.txt", Encoding.UTF8)) //从文件text2.txt中读取,需要注意:文件的编码格式,
{
strStream = streamReader.ReadToEnd();
}
Console.WriteLine(strStream);作者: 孙秋生 时间: 2012-3-20 17:03
StreamReader 旨在以一种特定的编码输入字符,而 Stream 类用于字节的输入和输出;使用 StreamReader 读取标准文本文件的各行信息。
用法:
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}