Sai A Sai A
Updated date Jun 13, 2023
In this blog, we will provide a detailed overview of various file handling techniques in C#. From reading and writing text files to handling binary and JSON files, it covers essential methods, code examples, and explanations to help you master file handling in C#.

Introduction:

File handling is an essential aspect of programming when it comes to dealing with data storage and retrieval. In C#, developers have several methods and techniques to read and write files efficiently. In this blog, we will explore different approaches and provide practical examples to demonstrate how to perform file handling operations in C#. From basic file reading and writing to more advanced techniques, we will cover everything you need to know to handle files effectively in your C# applications.

Method 1: Reading and Writing Text Files using StreamReader and StreamWriter

One of the simplest and most common methods to read and write text files in C# is by using the StreamReader and StreamWriter classes. These classes provide convenient methods for reading and writing strings to and from files. We can create an instance of the StreamReader or StreamWriter class, specify the file path, and use the ReadLine or WriteLine methods to perform the desired operations. The following code snippet demonstrates how to read and write text files using StreamReader and StreamWriter:

// Reading from a text file
using (StreamReader sr = new StreamReader("input.txt"))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

// Writing to a text file
using (StreamWriter sw = new StreamWriter("output.txt"))
{
    sw.WriteLine("Hello, World!");
}

Method 2: Reading and Writing Binary Files using BinaryReader and BinaryWriter

Binary files store data in a more compact and efficient manner compared to text files. C# provides the BinaryReader and BinaryWriter classes to read and write binary files, respectively. These classes allow you to read and write different data types, such as integers, floats, and byte arrays. The following code demonstrates how to read and write binary files using BinaryReader and BinaryWriter:

// Reading from a binary file
using (BinaryReader br = new BinaryReader(File.OpenRead("input.bin")))
{
    int number = br.ReadInt32();
    float floatValue = br.ReadSingle();
    byte[] byteArray = br.ReadBytes(10);
}

// Writing to a binary file
using (BinaryWriter bw = new BinaryWriter(File.OpenWrite("output.bin")))
{
    int number = 42;
    float floatValue = 3.14f;
    byte[] byteArray = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    bw.Write(number);
    bw.Write(floatValue);
    bw.Write(byteArray);
}

Method 3: Reading and Writing JSON Files using Newtonsoft.Json

JSON (JavaScript Object Notation) is a popular data interchange format. C# provides the Newtonsoft.Json library (also known as Json.NET) to serialize and deserialize JSON data. This library makes it easy to read and write JSON files in C#. Here's an example of reading and writing JSON files using Newtonsoft.Json:

// Reading from a JSON file
string jsonData = File.ReadAllText("input.json");
var jsonObject = JsonConvert.DeserializeObject<MyClass>(jsonData);

// Writing to a JSON file
var myObject = new MyClass { Name = "John Doe", Age = 30 };
string outputJson = JsonConvert.SerializeObject(myObject);
File.WriteAllText("output.json", outputJson);

Conclusion:

In this blog, we explored various methods of file handling in C#. We covered reading and writing text files using StreamReader and StreamWriter, reading and writing binary files using BinaryReader and BinaryWriter, and reading and writing JSON files using Newtonsoft.Json. Each method offers its own advantages depending on the type of data and the requirements of your application.

Comments (0)

There are no comments. Be the first to comment!!!