File handling is a fundamental operation in programming. In this article, we will explore how to read text file and write text file in C#. By the end, you'll understand essential C# file handling techniques with examples of read write operations. Whether you're new to C# file input output or looking for a text file handling tutorial, this guide is perfect for you.
File handling in C# involves performing text file operations like reading and writing data. C# provides classes like File, StreamReader, and StreamWriter for seamless text file manipulation.
To read text file, C# offers multiple methods. Below are two popular approaches:
This method reads the entire content of a file into a string.
using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Read the entire file content string content = File.ReadAllText(filePath); Console.WriteLine("File Content:"); Console.WriteLine(content); } }
The StreamReader class allows for line-by-line reading, which is useful for large files.
using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Read file line by line using (StreamReader reader = new StreamReader(filePath)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } }
Writing data to a text file is another crucial part of C# file handling. Below are two common methods:
This method writes a string to a file, overwriting its content if it already exists.
using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; string content = "Hello, World!"; // Write content to file File.WriteAllText(filePath, content); Console.WriteLine("File written successfully."); } }
The StreamWriter class enables appending data or writing line-by-line.
using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Write line by line to the file using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("Line 1"); writer.WriteLine("Line 2"); } Console.WriteLine("Data written successfully."); } }
You can easily combine read write operations to modify files. Here’s an example:
using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Read content string content = File.ReadAllText(filePath); Console.WriteLine("Original Content:"); Console.WriteLine(content); // Modify and write back string updatedContent = content + "\nAdditional Line"; File.WriteAllText(filePath, updatedContent); Console.WriteLine("Content updated successfully."); } }
This guide has provided a comprehensive overview of C# file handling. By understanding read text file and write text file operations, you can efficiently manipulate files in your applications. The examples shared here highlight essential text file operations with practical use cases. Explore more about C# read write file methods to enhance your skills.
Use the File.Exists method to verify file existence. Example: if (File.Exists(filePath)).
File.WriteAllText overwrites the entire file content in one step, while StreamWriter allows for more control, such as appending or writing line-by-line.
Use the StreamWriter class with the append parameter set to true:
using (StreamWriter writer = new StreamWriter(filePath, true)) { writer.WriteLine("Appended Line"); }
No, binary files require classes like FileStream or BinaryReader and BinaryWriter for appropriate handling.
For large files, prefer line-by-line reading with StreamReader and buffered writing with StreamWriter to minimize memory usage.
Copyrights © 2024 letsupdateskills All rights reserved