File Access Through StreamReader and StreamWriter Classes
The System.IO Namespace class allows you to read and write characters to and from files as streams, or contiguous groups of data, using specific encoding to convert characters to and from bytes. It includes a pair of classes, StreamReader and StreamWriter, which enable you to read or write a sequential stream of characters to or from a file. The StreamReader and StreamWriter classes mirror the functionality of the BinaryReader and BinaryWriter classes, but they read and write information as text rather than binary. For more information, see File Access Through BinaryReader and BinaryWriter Classes.
The StreamReader class is derived from an abstract class called TextReader, which reads characters from a stream or file. The StreamWriter class is derived from an abstract class called TextWriter, which writes characters to a stream or file.
Security Note When writing to files, an application may need to create a file if the file to which it is trying to write does not exist. To do so, it needs permission for the directory in which the file is to be created. However, if the file already exists, the application only needs Write permission to the file itself. Wherever possible, it is more secure to create the file during deployment and only grant Write permission to that file, rather than to the entire directory. It is also more secure to write data to user directories than to the root directory or the Program Files directory.
Reading From and Writing to Files
The following example writes a line of text to a file.
' Write text to a file Sub WriteTextToFile() Dim file As New System.IO.StreamWriter("c:\test.txt") file.WriteLine("Here is the first line.") file.Close() End Sub
The following example reads the text from a file into one string variable and writes the text to the console.
Sub ReadTextFromFile() Dim file As New System.IO.StreamReader("c:\test.txt") Dim words As String = file.ReadToEnd() Console.WriteLine(words) file.Close() End Sub
The following example adds text to an existing file.
Sub AppendTextToFile() Dim file As New System.IO.StreamWriter("c:\test.txt", True) file.WriteLine("Here is another line.") file.Close() End Sub
The following example reads one line at a time from a file and prints each line to the console.
Sub ReadTextLinesFromFile() Dim file As New System.IO.StreamReader("c:\test.txt") Dim oneLine As String oneLine = file.ReadLine() While (oneLine <> "") Console.WriteLine(oneLine) oneLine = file.ReadLine() End While file.Close() End Sub
Both the StreamReader and StreamWriter classes use UTF-8 encoding by default. UTF-8 encoding handles Unicode characters correctly and ensures that localized versions of the operating system are consistent with each other.
You can detect a file's encoding automatically with StreamReader, or you can specify it as a parameter on a constructor.
StreamWriter takes an encoding parameter on its constructor. If encoding is specified, Visual Basic writes to the file to indicate the encoding used.
File Access with Visual Basic .NET | File Access Through BinaryReader and BinaryWriter Classes | File Access Through the FileStream Class | Drive and Folder Access | System.IO Namespace | Code: Reading from a Text File (Visual Basic) | Code: Writing Text to a File (Visual Basic)