filestream(File Stream A Comprehensive Guide to File Handling in C#)

jk 416次浏览

最佳答案File Stream: A Comprehensive Guide to File Handling in C# When developing applications in C#, handling files is a common requirement. Files can be used for stor...

File Stream: A Comprehensive Guide to File Handling in C#

When developing applications in C#, handling files is a common requirement. Files can be used for storing data, configurations, logs, images, and much more. The .NET framework provides several classes for handling files, one of which is FileStream. In this article, we will explore the use of FileStream and its different functionalities.

What is FileStream?

FileStream is a class that allows for reading and writing to files in a low-level fashion. It exposes methods to read and write bytes, seek through the file, and truncate the file to a specific length. FileStream can be used to read and write to any type of file, including binary files like images and videos.

When creating a FileStream object, we need to specify the path to the file, the file mode (reading or writing), the file access mode (read-only, write-only, or read-write), and the file share mode (none, read, write, or delete).

Reading from a File using FileStream

The Read method of FileStream is used to read data from a file. It takes an array of bytes and the number of bytes to be read. The method returns the number of bytes actually read. The Read method starts reading from the current position of the file pointer and advances the pointer by the number of bytes read.

For example, to read the contents of a text file into a string, we can use the following code:

using System.IO;
string path = \"myfile.txt\";
string contents;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    byte[] buffer = new byte[fs.Length];
    int bytesRead = fs.Read(buffer, 0, (int)fs.Length);
    contents = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
}

Writing to a File using FileStream

The Write method of FileStream is used to write data to a file. It takes an array of bytes and the number of bytes to be written. Like the Read method, it starts writing at the current position of the file pointer and advances the pointer by the number of bytes written.

For example, to write a string to a text file, we can use the following code:

using System.IO;
string path = \"myfile.txt\";
string contents = \"Hello, World!\";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(contents);
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
    fs.Write(buffer, 0, buffer.Length);
}

Conclusion

Handling files is an essential part of programming. In C#, the FileStream class provides a way to read and write to files at a low level. We learned about some of the methods exposed by FileStream and saw how we can use them to read and write to text files. Similar techniques can be used to read and write to binary files. Now that you have a good understanding of FileStream, you can start using it in your own C# projects.