0% found this document useful (0 votes)
13 views2 pages

Reading From A File Line by Line

The document explains how to read data from files in Visual Basic, focusing on text files and the use of the StreamReader object. It outlines the steps to read a file line by line, including initializing the StreamReader, reading data with the ReadLine method, and closing the file after reading. Additionally, it provides important do's and don'ts to consider when handling files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Reading From A File Line by Line

The document explains how to read data from files in Visual Basic, focusing on text files and the use of the StreamReader object. It outlines the steps to read a file line by line, including initializing the StreamReader, reading data with the ReadLine method, and closing the file after reading. Additionally, it provides important do's and don'ts to consider when handling files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Reading from a file

A file is a collection of data stored in computer memory with a specific name and a defined folder
path. A path is used to perform operations on the path of a file.

There are different types of files which are: TXT, PDF, DOC and DOCX.

TO READ FROM A FILE LINE BY LINE

Visual Basic allows us to get input from different types of files and it also allow us to write output to
different types of files. We will talk about text files only. Firstly, we create object of file reader to read
some text from a file we created. A file must exist before reading it because if it does not exist, an
empty file will be created.

There are many ways we can read from a file like, reading from a file line by line and reading a
delimited file.

Reading from a file line by line.

We can only read from a file line by line if the file contains only one item per line. The read line
method reads the text, starting from the current position and continuing until the end of the line is
counted. Reading from a file depends on how the data is stored in a file. However, we must keep in
mind that there is a limited number of data values in a file, but we do not know how many values
there in a file. Therefore, we typically use a while loop that test for the end of the file when we
process a file.

STEP 1

To read from a file we use a data type object called streamreader which is from the inputoutput class
that can read stream of characters coming from memory location.

Dim variable name as IO. StreamReader

Variable name= IO.File.OpenText(filespec)

File specification identifies the file to be read. This establishes a communication link between the
computer and the disk drive for reading data from the disk drive.

STEP 2

Reading a data item from a file is done using the readline builtin function.

StrVar= readerVar.Readline.

The data can be assigned to a numeric variable, in that case we use Val function.

numVar= Val (readerVar.ReadLine)

When streamReader is initialized an input marker is positioned at the beginning of the file, then if
you reach the end of the file the marker is moved to the next line. After the last line is read, the
marker is said to be at the end of the file. The StreamReader has a Boolean function called
Endofstream. The Endofstream is vital if a file contains an unknown number of items, and you want
to read all the information.

STEP 3

CLOSING THE FILE


After reading from the file, the last step is to terminate the communication link that was set up in
Step 1.

readVar.Close()

DO’S AND DONT’S

Do not make decisions about the contents of the file based on the name of the file.

Do not assume that the file is in a specific format.

Do not assume that the file is small enough to fit into memory.

Do not forget to handle exceptions that may occur while reading the file.

You might also like