You are currently viewing Beginner’s Guide to File Input/Output (I/O) in C# with Examples

Beginner’s Guide to File Input/Output (I/O) in C# with Examples

  • Post author:
  • Post category:C#
  • Post comments:0 Comments
  • Post last modified:May 16, 2024

Introduction to File Input/Output (I/O) in C

File Input/Output (I/O) operations are essential for any programming language, allowing you to interact with files on disk. In C#, the .NET framework provides robust libraries for handling File I/O tasks efficiently. This tutorial will guide you through the basics of File I/O in C#, covering reading from and writing to files with practical examples.

1. Reading from a File

Reading Text Files

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "sample.txt";

        try
        {
            // Read all lines from the file
            string[] lines = File.ReadAllLines(filePath);

            // Display lines
            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }
        }
        catch (IOException e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }
    }
}

Reading Binary Files

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "binary.bin";

        try
        {
            // Read all bytes from the file
            byte[] data = File.ReadAllBytes(filePath);

            // Process data
            // Example: Display byte count
            Console.WriteLine("Byte count: " + data.Length);
        }
        catch (IOException e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }
    }
}

2. Writing to a File

Writing Text to a File

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "output.txt";

        try
        {
            // Write text to the file
            string text = "Hello, File I/O!";
            File.WriteAllText(filePath, text);

            Console.WriteLine("Data written to file successfully.");
        }
        catch (IOException e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }
    }
}

Writing Binary Data to a File

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "output.bin";

        try
        {
            // Create binary data
            byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F }; // "Hello" in ASCII

            // Write data to the file
            File.WriteAllBytes(filePath, data);

            Console.WriteLine("Data written to file successfully.");
        }
        catch (IOException e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }
    }
}

Conclusion

Congratulations! You’ve learned the basics of File Input/Output (I/O) in C#. With the examples provided, you should now be equipped to read from and write to files in your C# applications. Explore further by diving into more advanced file manipulation techniques and error handling strategies. Happy coding!

Leave a Reply