StreamReader和StreamWriter是C#中用于处理文本文件的类,而FileStream是用于处理二进制文件的类。下面是它们的用法详解:
StreamReader用法:
StreamReader reader = new StreamReader("file.txt");
StreamReader reader = new StreamReader(File.OpenRead("file.txt"));
string line = reader.ReadLine();
string content = reader.ReadToEnd();
reader.Close();
StreamWriter用法:
StreamWriter writer = new StreamWriter("file.txt");
StreamWriter writer = new StreamWriter(File.OpenWrite("file.txt"));
writer.Write("Hello");
writer.WriteLine("World");
writer.Close();
FileStream用法:
FileStream fileStream = new FileStream("file.bin", FileMode.Open, FileAccess.ReadWrite);
byte[] buffer = new byte[1024];
int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
byte[] data = { 0x01, 0x02, 0x03 };
fileStream.Write(data, 0, data.Length);
fileStream.Close();
以上就是StreamReader/StreamWriter与FileStream的用法详解。根据具体需求和文件类型选择适合的类来处理文件操作。