using System; using System.IO;public class TextFromFile { private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args) { if (!File.Exists(FILE_NAME)) { Console.WriteLine("{0} does not exist.", FILE_NAME); return; } using (StreamReader sr = File.OpenText(FILE_NAME)) { String input; while ((input=sr.ReadLine())!=null) { Console.WriteLine(input); } Console.WriteLine ("The end of the stream has been reached."); sr.Close(); } } }
using System; using System.IO;public class TextFromFile { private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args) { if (!File.Exists(FILE_NAME)) { Console.WriteLine("{0} does not exist.", FILE_NAME); return; } using (StreamReader sr = File.OpenText(FILE_NAME)) { String input; while ((input=sr.ReadLine())!=null) { Console.WriteLine(input); Console.ReadLine(); } Console.WriteLine ("The end of the stream has been reached."); sr.Close(); } } }
wuyi8808 的写法好像不错哦
StreamReader sr = new StreamReader(); sr.EndOfStream;这是返回流是否读到末端,可以配合readline()使用
using System.IO;public class TextFromFile
{
private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine ("The end of the stream has been reached.");
sr.Close();
}
}
}
using System.IO;public class TextFromFile
{
private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
Console.ReadLine();
}
Console.WriteLine ("The end of the stream has been reached.");
sr.Close();
}
}
}
sr.EndOfStream;这是返回流是否读到末端,可以配合readline()使用
System.IO.File.ReadAllLines("path");