File Pointer

gorilla

Member
Joined
Jun 12, 2003
Messages
12
I need some help on opening files and pointing to particular lines in files


For example I had a text file called
text1.txt
----start of file-----
one
two
three
four
five
---end of file----

How can I copy the entire third line into a file called output.txt?

so the outcome of this command would be

output.txt
---start of file---
three
---end of file---

how would i implement something like this in VB? thanks for your time :)
 
The StreamReader class has a ReadLine method, which reads the
file one line at a time. Create a new FileStream to the file, make
a new StreamReader to read that stream, then read the line
twice to advance it two lines. Then read the third line, set it to a
var, and close all the streams.

Make sure you import System.IO
Code:
Dim fs As New FileStream("file path")
Dim reader As New StreamReader(fs)
Dim i As Integer
Dim thirdLine As String

reader.ReadLine()  Read the first line
reader.ReadLine()  Read the second line

thirdLine = reader.ReadLine()  Read the third line and set it to a variable

 Clean up
reader.Close()
fs.Close()

If you wanted, you could also read the entire file into a variable,
split it up by newline characters in an array, then access the
specific array element to get that line.
 
Back
Top