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

Writing to Files with StreamWriter

The document explains the process of outputting data to a file using StreamWriter in Visual Basic. It outlines the steps of opening a file, writing data using Write and Write.Line functions, and closing the file to ensure data is saved. Key points include the importance of using 'True' when initializing StreamWriter and the necessity of closing the file after writing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Writing to Files with StreamWriter

The document explains the process of outputting data to a file using StreamWriter in Visual Basic. It outlines the steps of opening a file, writing data using Write and Write.Line functions, and closing the file to ensure data is saved. Key points include the importance of using 'True' when initializing StreamWriter and the necessity of closing the file after writing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Output to a File

What is an output ?

Output means to produce results, results that can be displayed on screen, written to a printer or to a
file, in this topic we will be focusing on outputting to a file.

Which file are we speaking about?

A delimited file, Textfiles,

So for us to actually produce results or rather have an output, we have steps to follow,

Step 1

Opening a File.

To write to a File we use a data type called StreamWriter.

A streamWriter writes data to a text file. It can write lines of text or strings to the file. It is easy to use
and one of the most common classes in the Visual basics Programming network.

The streamWriter is Declared in the following Format : FileWriter As IO.StreamWriter


Where : FileWriter(Variable)
IO(Input Output Class)
Streamwriter(Datatype)

To initialise the Streamwriter we need to give it the file name and location in
following manner
FileWriter =New System.IO.Streamwriter(“FileName.Txt”,True)

This can be initialised in one line like other Variables in the following way
Dim FileWriter As IO.StreamWriter =New
System.IO.Streamwriter(“FileName.Txt”,True)
Do’s and Don’ts
You must always write True after the mentioning of the file name so that the
program can execute even if the file does not exist, Visual basic will create a file
for you. If you write false the program will not execute.

Step 2 : Writing data to the File


When we’re inserting Data to the file we use the Write Function or the Write.Line
Function
The write Function will write all the data to a sequential file without separating
the data since the write function does not separate the data you use the
write.Line Function which writes data in the textfile line by line
The Write Function has the following format:
fw.Write=str(or num)
The Write.Line function has the following format different from the write function
fw.Write.Line=str(or num)
you can either use the str or num, where str is string and num stands for
numerical value.
Do’s and don’ts
It advisable to use Write.Line function when you having a lot of information to
insert into your file that uses different lines or rather have separators
For example when you want to write the list of students taking the CSC module,
you will use the Write. Line function.

Step3: Close the file


After you have written all the data you want to the file, you close the file using
the following format:
fw.close()
do’s and don’ts
it is imperative to close the file as it makes sure that all your data is saved. If you
don’t close the file, all the information you have written will erased.
.

You might also like