/*readalltext 读取完直接显示出来,只能用于读取文本文件*/
string p= File.ReadAllText(@"C:\Users\Administrator.SC-201807150713\Desktop\新建文本文档.txt",Encoding.Default );
for (int i = 0; i < p.Length; i++)
{
Console.Write(p[i]);
}
Console.ReadKey();
/*ReadAllLines一行一行的读取,只能用于读取文本文件*/
string [] f= File.ReadAllLines(@"C:\Users\Administrator.SC-201807150713\Desktop\新建文本文档.txt",Encoding.Default);
for (int i = 0; i < f.Length; i++)
{
Console.Write(f[i]);
}
Console.ReadKey();
/*ReadAllBytes读取到的是二进制字节,需要解码,可以读取任何文件*/
byte[] b= File.ReadAllBytes(@"C:\Users\Administrator.SC-201807150713\Desktop\新建文本文档.txt");
string s= Encoding.Default.GetString(b);
Console.Write(s);
Console.ReadKey();
/*FileStream读取文件可以读取任何文件*/
using (FileStream fs = new FileStream(@"C:\Users\Administrator.SC-201807150713\Desktop\查询出库单对应的核算体系成本.txt", FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 2];
while (true)
{
int c = fs.Read(buffer, 0, buffer.Length);
string s= System.Text.Encoding.Default.GetString(buffer, 0, c);
Console.WriteLine(s);
if (c == 0)
{
break;
}
}
Console.ReadKey();
}
/*StreamReader 读取字符文件*/
using (StreamReader sr = new StreamReader(@"C:\Users\Administrator.SC-201807150713\Desktop\查询出库单对应的核算体系成本.txt"))
{
while (true)
{
String s = sr.ReadLine();
Console.WriteLine(s);
if (sr.EndOfStream)//如果是结尾
{
break;
}
}
Console.ReadKey();
}