Reading and Writing Text Files in One Command
Reading and writing text files is a relatively simple process when using the StreamReader and StreamWriter classes. When you are working with small text files and require less flexibility, you can read or write an entire file using just one statement.
Reading and Writing Text Files
In two recent articles I described the process of reading text file information using the StreamReader class and writing to a text file using the StreamWriter class. These two classes provide methods that allow great flexibility when working with text files, allowing you to control the format of information created, append to existing files and read small sections of a large file without requiring the allocation of lots of memory.
When you are working with small text files, this level of flexibility may be unnecessary. Often, you will simply wish to read an entire text file into a string for further processing or store the contents of a string in a file on disk. In these situations you can use members of the File class to create or read an entire file with a single line of code.
The File class is a standard class in the System.IO namespace. It provides static methods for working with the file system. In the .NET framework version 2.0, Microsoft introduced two new members, named "WriteAllText" and "ReadAllText" that simplify the process of storing and retrieving strings using text files. WriteAllText allows a string to be saved into a new text file, whilst ReadAllText loads the entire contents of a text file into a string instance. Both methods include the opening and closing of the file and the releasing of resources to provide an atomic process.
To follow the examples later in the article, add the following using directive to allow simplified references to the File class.
There are some limitations of the ReadAllText and WriteAllText methods. One of the key limitations when reading text files is that the entire content of the file is retrieved and held in memory. When working with large files this can cause out-of-memory exceptions and performance problems. When writing files, the only options are to create a new file or overwrite an existing one. There is no facility to append to a file. If these limitations are too restrictive, you should use an alternative technique to read or write files.
Writing a Text File
The simplest way to write to a text file using the WriteAllText method is by specifying two parameters. The first parameter is a string containing the path and name of the file to create. If a file with the same name already exists it will be overwritten. The second argument is the string containing information to store within the text file.
The example below creates a string containing three lines of text. This is then stored in a file named "test.txt" within the "c:\temp" folder. Ensure that a matching file does not already exist before trying the code.
string test = @"Line 1
The text in a text file can be encoding using various methods, such as UTF or Unicode. If not specified, text will be written using UTF-8 encoding. If your application requires that you use an alternative, you can add a constant from the Encoding enumeration as a third parameter. For example, to store the text using Unicode, you could use the following code:
File.WriteAllText("c:\\temp\\test.txt", test, Encoding.Unicode);
Reading a Text File
To read the entire contents of a text file into a string you can use the ReadAllText method. This method requires a single argument, used to specify the path and filename of the file to be loaded. The method returns the populated string.
To retrieve the previously created sample file, you can execute the following:
string read = File.ReadAllText("c:\\temp\\test.txt");
If you need to specify the encoding for a file whilst reading, add the encoding type as a second parameter. For example:
string read = File.ReadAllText("c:\\temp\\test.txt", Encoding.Unicode);
27 September 2009