Home
Map
File HandlingHandle files: in the System.IO namespace, use StreamReader and StreamWriter.
VB.NET
This page was last reviewed on Nov 21, 2023.
File. In the VB.NET language, files are written and read. With the File type, we perform efficient manipulations of files, including reads, writes and appends.
Background. The System.IO namespace deals with files. StreamReader is often the simplest way to read in a text file. And StreamWriter writes out text.
StreamReader. We often employ a using-construct to create a StreamReader object. StreamReader is ideal for looping over the lines in a file. It can also result in less memory usage.
StreamReader
StreamWriter
Step 1 We use the StreamReader. It is best to use the Using statement, which provides for system-level cleanup of resources.
Step 2 Here we begin using the StreamReader inside a Do While (True) loop construct.
Step 3 We read in a line with ReadLine() and test to see if the string is Nothing. We exit if it is Nothing.
Imports System.IO Module Module1 Sub Main() ' Step 1: create StreamReader for the file. Using reader As StreamReader = New StreamReader("C:\programs\file.txt") ' Step 2: begin do-loop. Do ' Step 3: call ReadLine. Dim line As String = reader.ReadLine ' ... Exit the loop if Nothing. If line Is Nothing Then Exit Do End If Console.WriteLine(line) Loop End Using End Sub End Module
Thank you Friend
ReadAllText. We use this function to read an entire text file into a String. Please note that this function is implemented in terms of the other methods in the System.IO namespace.
File.ReadAllText
Thus You could implement a function equivalent to ReadAllText with the StreamReader type.
Imports System.IO Module Module1 Sub Main() Dim value As String = File.ReadAllText("C:\file.txt") Console.WriteLine(value.Length) End Sub End Module
40
ReadAllLines. This Function reads in a text file and places each line in the file in a String array. The array is like any other array, and can be converted or modified.
Step 1 We call ReadAllLines with an argument equal to the file name. It places the lines in a String array.
Step 2 A For-Each loop is used on that String, providing each line in the file for usage elsewhere in code.
Info File.ReadLines is different from ReadAllLines. It reads one line in at a time—this uses less memory.
File.ReadLines
Imports System.IO Module Module1 Sub Main() ' Step 1: read the file into an array. ' ... Create the required file if needed. Dim array As String() = File.ReadAllLines("file.txt") ' Step 2: use For-Each loop. For Each line As String In array ' Use the line. Dim length As Integer = line.Length Next End Sub End Module
List. Often we use Lists of strings. But the File.ReadAllLines method returns a String array. You can convert the result of File.ReadAllLines into a List with ToList.
Then You can use each line of the file in a List collection. You can apply all List functions.
List
Imports System.IO Module Module1 Sub Main() ' Create a list reference variable. ' ... Then read all lines in the file. ' ... Convert the array to a List. Dim list As List(Of String) = File.ReadAllLines("file.txt").ToList Console.WriteLine(list.Count) End Sub End Module
5
Line count. There are many ways to acquire the line count of a file. In this example, we see that you can actually load the file into an array, and then get the Length of that array.
Tip You could instead use StreamReader code above, and then increment an integer on each successful call to the ReadLine method.
Imports System.IO Module Module1 Sub Main() ' Get the length of the file. ' ... Not the fastest way to get the line count. Dim length As Integer = File.ReadAllLines("file.txt").Length Console.WriteLine(length) End Sub End Module
5
WriteAllLines. It is possible to take a String array and write it as lines to a file. We use File.WriteAllLines, a shared method. In this example, the output file will contain three lines.
Imports System.IO Module Module1 Sub Main() ' Create an array of three elements. Dim array As String() = New String() {"cat", "dog", "arrow"} ' Write the array to a file. File.WriteAllLines("file.txt", array) End Sub End Module
cat dog arrow
ReadAllBytes. We can invoke File.ReadAllBytes to get a Byte array from a file. Here we read in a WEBP file and print its Length and first byte value.
Imports System.IO Module Program Sub Main() ' Read in binary file with ReadAllBytes. Dim webpFile() As Byte = File.ReadAllBytes("C:\programs\test.webp") Console.WriteLine("Length: {0}", webpFile.Length) Console.WriteLine("First byte: {0}", webpFile(0)) End Sub End Module
Length: 822 First byte: 82
With clear code, such as the Using statement, we read and write (mutate) files. Often we handle text files. But these functions can also act on images or binary data.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 21, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.