How to use file IO and stream operations to read and write data in C# requires specific code examples
In C# programming, file IO and stream operations are commonly used technologies , used to read and write data from files. Whether processing text files, binary files, or reading network stream data, we can do it through file IO and stream operations.
File IO and stream operations provide a flexible way to process data, and can read or write any type of data, making it easier for us to process files and data streams in our programs.
The following will introduce in detail how to use C# for file IO and stream operations, and give some specific code examples.
1. File IO operations
File IO operations in C# can be implemented using the classes provided by the System.IO namespace. The following are some commonly used file IO operation methods:
string filePath = "D:\test.txt"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { // 执行文件操作 }
string filePath = "D:\test.txt"; using (StreamReader streamReader = new StreamReader(filePath)) { string content = streamReader.ReadToEnd(); Console.WriteLine(content); }
string filePath = "D:\test.txt"; string content = "Hello, World!"; using (StreamWriter streamWriter = new StreamWriter(filePath)) { streamWriter.Write(content); }
string filePath = "D:\test.bin"; using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); // 处理二进制数据 }
string filePath = "D:\test.bin"; byte[] data = { 0x01, 0x02, 0x03, 0x04 }; using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { fileStream.Write(data, 0, data.Length); }
2. Stream operation
A stream is an abstraction of data. Data can be read from a stream or written to a stream. In C#, you can use the stream classes provided by the System.IO namespace to implement stream operations.
string filePath = "D:\test.txt"; using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { using (StreamReader streamReader = new StreamReader(fileStream)) { string content = streamReader.ReadToEnd(); Console.WriteLine(content); } }
string filePath = "D:\test.txt"; string content = "Hello, World!"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { using (StreamWriter streamWriter = new StreamWriter(fileStream)) { streamWriter.Write(content); } }
using (TcpClient client = new TcpClient("127.0.0.1", 8080)) { using (NetworkStream networkStream = client.GetStream()) { byte[] buffer = new byte[1024]; int bytesRead = networkStream.Read(buffer, 0, buffer.Length); string response = Encoding.UTF8.GetString(buffer, 0, bytesRead); Console.WriteLine(response); } }
using (TcpClient client = new TcpClient("127.0.0.1", 8080)) { using (NetworkStream networkStream = client.GetStream()) { string request = "Hello, Server!"; byte[] buffer = Encoding.UTF8.GetBytes(request); networkStream.Write(buffer, 0, buffer.Length); } }
The above are sample codes for some basic file IO and stream operations. By using these codes, we can easily read and write file data, or process network stream data. Depending on specific needs, we can flexibly choose to use the file IO operation method or the stream operation method.
Summary:
File IO and stream operations in C# provide a flexible and powerful way to handle files and data streams. Whether it is reading and writing files, or processing network stream data, we only need to use the appropriate file IO operation method or stream operation method to complete the corresponding data reading and writing operations. Mastering these technologies is very important for developing applications with file and data stream processing capabilities.
The above is the detailed content of How to use file IO and stream operations to read and write data in C#. For more information, please follow other related articles on the PHP Chinese website!