Reading / Writing Text Files.
.Net makes it easy to read and write text files. All the hard work is done in the framework, and the necessary processes are exposed as methods in the My.Computer.FileSystem class.
A text file is any file that consists of plain text. It is usually separated out into lines. The files may contain descriptive text or data stored as text, they might be unformatted or formatted, for instance as CSV, fixed width (columns), or any other style. The only requirement is that the processing of the file does not depend on any particular byte values for any part of the contents – the content will be handled as strings of text and analysed (if required) as strings and characters.
This example is for reading and writing a plain text file.
Dim InPath As String = System.IO.Path.Combine( _ My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Input.txt") Dim FileText As String = My.Computer.FileSystem.ReadAllText(InPath)
The ReadAllText method opens the file, reads the contents into the string variable and closes the file. That’s all there is to it!
If the file consists of multiple lines, then it can be read into a string array where each line is an element in the array. The ReadAllLines method is similar to ReadAllText except that it dissects the text into lines and returns an array of string, where each line is one element in the array. Note that the end-of-line characters are not included in the strings.
Dim InPath As String = System.IO.Path.Combine( _ My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Input.txt") Dim Lines() As String = My.Computer.FileSystem.ReadAllLines(InPath)
If the file contained lines with formatted data such as CSV, then further processing of the lines would look like this:
For Each Line As String In Lines Dim LineItems() As String = Line.Split(","c) For Each LineItem As String In LineItems 'Process the line item here Next Next
(Note that there is a TextFieldParser class that can do this parsing for you, although the above code is so simple it hardly seems worthwhile.)
To write the contents back out to the file the process is reversed:
Dim OutPath As String = System.IO.Path.Combine( _ (My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Output.txt") My.Computer.FileSystem.WriteAllText( _ OutPath, FileText, False)
Or, using the WriteAllLines method to write an array of text lines:
Dim OutPath As String = System.IO.Path.Combine( _ (My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Output.txt") My.Computer.FileSystem.WriteAllLines(OutPath, Lines)
If the output file doesn’t exist, it will be created. The False value for the third parameter in the WriteAllText method means that if the output file already exists, it will be overwritten: True would mean that the data was appended to the current contents of the file. The WriteAllLines method always overwrites any existing file.
There are many other ways to read and write a text file, but the ReadAllText/ReadAllLines and WriteAllText/WriteAllLines methods of the My.Computer.FileSystem class are the simplest to use.