StreamReader的几种常用方法
1 string path = @"D:\test.txt"; 2 FileStream filestream = new FileStream(path, FileMode.Open); 3 4 5 using (FileStream stream = File.OpenRead(path)) 6 { 7 using (StreamReader reader = new StreamReader(stream)) 8 { 9 Console.Write(reader.ReadToEnd()); 10 Console.WriteLine("\r\n****************"); 11 } 12 } 13 14 using (FileStream stream = File.OpenRead(path)) 15 { 16 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, false)) 17 { 18 Console.Write(reader.ReadToEnd()); 19 Console.WriteLine("\r\n****************"); 20 } 21 } 22 23 using (StreamReader reader = new StreamReader(path, Encoding.Default, false, 3)) 24 { 25 Console.Write(reader.ReadToEnd()); 26 Console.WriteLine("\r\n****************"); 27 } 28 29 using (StreamReader reader = File.OpenText(path)) 30 { 31 Console.Write(reader.ReadToEnd()); 32 Console.WriteLine("\r\n****************"); 33 }
就方便而言,最后一种是最方便的。