C#编程-119:读取文件StreamReader类
分类:
文章
•
2024-08-04 17:49:52
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.IO;
-
namespace StreamReaderTest
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
string path = @"C:\Windows\System32\drivers\etc\hosts";
-
string myline = "";
-
if (File.Exists(path))
-
{
-
//using(){}自动帮助我们释放流所占用的空间
-
//()创建过程
-
//{}读取或写入的过程
-
//均不能加分号
-
-
using (StreamReader sr = new StreamReader(path))
-
{
-
myline = sr.ReadLine();
-
while (myline != null)
-
{
-
Console.WriteLine(myline);
-
myline = sr.ReadLine();
-
}
-
}
-
}
-
else
-
{
-
Console.WriteLine("文件不存在!");
-
}
-
Console.ReadLine();
-
//方法一:
-
-
//方法二:
-
}
-
}
-
}