Reading a Text File Line by Line (2024)

Home and Learn: VB Net Course

This lesson is part of an ongoing tutorial. The first part is here: How to open a Text File in VB .NET

Quite often, you don't want to read the whole file at once. You want to read it line by line. In which case, instead of using the ReadToEnd method, as we did in the previous section, you can use the ReadLine method:

The ReadLine method, as its name suggests, reads text one line at a time. In order to do this, though, you need to use a loop. You can then loop round each line and read it into a variable. Here's a coding example:

Dim TextLine As String

Do While objReader.Peek() <> -1

TextLine = TextLine & objReader.ReadLine() & vbNewLine

Loop

The first line of the Do While loop is rather curious:

Do While objReader.Peek() <> -1

The Peek method takes a peek at the incoming text characters. It's looking ahead one character at a time. If it doesn't see any more characters, it will return a value of minus 1. This will signify the end of the text file. Our loop checks for this minus 1, and bails out when Peek has this value.

Inside the loop, we're reading each line from the text file and putting into new variable. (We're also adding a new line character on the end. Delete the & vbNewLine and see what happens).

objReader.ReadLine()

So the ReadLine method reads each line for you, instead of the ReadToEnd method which gets the whole of the text file.

Once you have a line of text in your variable, though, it's up to you to parse it. For example, suppose the line of text coming in from the text file was this:

"UserName1, Password1, UserName2, Password2"

See Also
type

You would then have to chop the line down and do something which each segment. VB won't do this for you! (But you saw how to do this in the last section, when you used things like Split and Substring.)

But what you are doing in the Do Loop is building up your variable with lines of text that are pulled from your text file. Once you have pulled all the text from your file, you can then put it into the text box. Here's our programme once more:

Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"

Dim TextLine As String

If System.IO.File.Exists( FILE_NAME ) = True Then

Dim objReader As New System.IO.StreamReader(FILE_NAME)

Do While objReader.Peek() <> -1

TextLine = TextLine & objReader.ReadLine() & vbNewLine

Loop

Textbox1.Text = TextLine

Else

MessageBox.Show("File Does Not Exist")

End If

So inside the loop, we go round building up the TextLine variable. Once all the file has been read (when Peek() has a value of -1), we then place it into Textbox1.

In the next part, you'll learn how to write to a text file.

Learn how to Write to a Text File with VB .NET -->

Back to the VB NET Contents Page

Reading a Text File Line by Line (2024)
Top Articles
Explained: What Are TikTok Coins and How Do They Work?
What Is TikTok? – Everything You Need to Know in 2024
3 Tick Granite Osrs
Poe T4 Aisling
Dragon Age Inquisition War Table Operations and Missions Guide
Rubratings Tampa
Davita Internet
Cintas Pay Bill
Health Benefits of Guava
Kansas Craigslist Free Stuff
1movierulzhd.fun Reviews | scam, legit or safe check | Scamadviser
Unlocking the Enigmatic Tonicamille: A Journey from Small Town to Social Media Stardom
Fnv Turbo
Computer Repair Tryon North Carolina
Craigslist Vermillion South Dakota
Mail Healthcare Uiowa
Elle Daily Horoscope Virgo
Funny Marco Birth Chart
Springfield Mo Craiglist
Stihl Km 131 R Parts Diagram
Ostateillustrated Com Message Boards
Violent Night Showtimes Near Amc Fashion Valley 18
Dark Chocolate Cherry Vegan Cinnamon Rolls
Cbssports Rankings
Maxpreps Field Hockey
1973 Coupe Comparo: HQ GTS 350 + XA Falcon GT + VH Charger E55 + Leyland Force 7V
How to Download and Play Ultra Panda on PC ?
Valic Eremit
15 Primewire Alternatives for Viewing Free Streams (2024)
Margaret Shelton Jeopardy Age
Violent Night Showtimes Near Johnstown Movieplex
Costco Jobs San Diego
Delete Verizon Cloud
Things to do in Pearl City: Honolulu, HI Travel Guide by 10Best
24 Hour Drive Thru Car Wash Near Me
Boondock Eddie's Menu
Does Iherb Accept Ebt
Merkantilismus – Staatslexikon
Craigslist - Pets for Sale or Adoption in Hawley, PA
Wal-Mart 140 Supercenter Products
Owa Hilton Email
Shipping Container Storage Containers 40'HCs - general for sale - by dealer - craigslist
Payrollservers.us Webclock
Linkbuilding uitbesteden
How I Passed the AZ-900 Microsoft Azure Fundamentals Exam
Goats For Sale On Craigslist
Canada Life Insurance Comparison Ivari Vs Sun Life
Sam's Club Gas Price Sioux City
Bama Rush Is Back! Here Are the 15 Most Outrageous Sorority Houses on the Row
Verilife Williamsport Reviews
The Missile Is Eepy Origin
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5770

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.