PDF Archive

Easily share your PDF documents with your contacts, on the Web and Social Networks.

Share a file Manage my documents Convert Recover PDF Search Help Contact



22 FileIOandStreams .pdf


Original filename: 22_FileIOandStreams.pdf
Title: Lesson 22 - File IO & Streams.pdf
Author: ict

This PDF 1.4 document has been generated by PrimoPDF http://www.primopdf.com / Nitro PDF PrimoPDF, and has been sent on pdf-archive.com on 11/02/2014 at 15:12, from IP address 46.60.x.x. The current document download page has been viewed 552 times.
File size: 119 KB (4 pages).
Privacy: public file




Download original PDF file









Document preview


Visual Basic

Project Name:

Lesson 22 – File IO & Streams

www.mrfraser.org

File Read and Write Methods

In this program you will learn how to use Regions to collapse code segments, force the use
of scrollbars in textboxes, and more importantly, how to create files and write to them /
read from them using File IO access methods and Streaming IO methods. You should begin
by creating the form below:

Components Used:
Component
Textbox
Button
Button
Button

Name
TextBox1
cmdTestFileIO
cmdTestStreams
cmdQuit

Use the following Code:
Imports System.IO

'imports file I/O functionality

Public Class Form1
#Region "Main Program"

'creates a new region to group segements of code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Show()
Me.Text() = "File Input & Output"
'set form title
Me.TextBox1.Font = New Font("Calibri", 12)
'set font face & size
Me.TextBox1.ScrollBars = ScrollBars.Vertical 'force vertical scrollbar
End Sub

1

Visual Basic

Lesson 22 – File IO & Streams

www.mrfraser.org

'//////////////////////////////////////////////////////////////////////
'//
'// Use File I/O Methods for read/write
'//
'// This procedure will create a new text file and populate it with
'// supplied text (in this case, supplied as part of the procedure
'// but the text could just as easily come from user input) and then
'// save the file. The file is then opened in read mode and its
'// contents displayed in a text box.
'//
Private Sub TestFileIO()
'declare & initialise variables
Dim fileName As String = ""
Dim inputText As String = ""
Dim inputLine As String = ""
Dim fileNumber As Integer = 0
'Set the fileName
fileName = Directory.GetCurrentDirectory() & "\ReadWrite.txt"
'find an unused file number
' you must request an available file number before attempting to open a
' file for reading or writing. Each open file is assigned a file number.
fileNumber = FreeFile()
'Open the text file for output
FileOpen(fileNumber, fileName, OpenMode.Output, OpenAccess.Write)
'Write some lines of text to the file
WriteLine(fileNumber, "Soup Soup Tasty Soup Soup")
WriteLine(fileNumber, "Spicy carrot and corriander")
WriteLine(fileNumber, "Chilli chowder")
WriteLine(fileNumber, "Crouton Crouton")
WriteLine(fileNumber, "Crunch friends in a liquid broth")
WriteLine(fileNumber, "I am gespatchio Oh!")
WriteLine(fileNumber, "I am a summer soup Mmmm!")
WriteLine(fileNumber, "Miso Miso")
WriteLine(fileNumber, "Fighting in the dojo")
WriteLine(fileNumber, "Miso Miso")
WriteLine(fileNumber, "Oriental Prince in the land of soup")
'Close the file
FileClose(fileNumber)
'find an unused file number
fileNumber = FreeFile()
'Open text file for input
FileOpen(fileNumber, fileName, OpenMode.Input, OpenAccess.Read)
'Read contents of file into a temporary variable
Do Until EOF(fileNumber)
inputLine = LineInput(fileNumber)
inputText += inputLine.Substring(1, inputLine.Length - 2) & vbCrLf
Loop
'Close the file
FileClose(fileNumber)

2

Visual Basic

Lesson 22 – File IO & Streams

www.mrfraser.org

'Display contents of the file
TextBox1.Text() = "Testing File I/O..." & vbCrLf
TextBox1.Text() &= inputText & vbCrLf
'Reomve the default text selection
TextBox1.Select(0, 0)
End Sub
'////////////////////////////////////////////////////////////////////
'//
'// Use I/O Stream Readers/Writers for read/write
'//
'// This procedure will create a new text file and populate it with
'// supplied text (in this case, supplied as part of the procedure
'// but the text could just as easily come from user input) and then
'// save the file. The file is then opened in read mode and its
'// contents displayed in a text box.
'//
Private Sub TestStreams()
'declare & initialise variables
Dim inFile As StreamReader = Nothing
Dim outFile As StreamWriter = Nothing
Dim fileName As String = ""
Dim inputText As String = ""
'Set the fileName
fileName = Directory.GetCurrentDirectory() & "\ReadWrite.txt"
'Create new streamwriter object
outFile = New StreamWriter(fileName)
'Add text to outfile using sreamwriter
outFile.WriteLine("Calm a llama down")
outFile.WriteLine("Calm a llama deep down")
outFile.WriteLine("In the ocean blue")
outFile.WriteLine("Like a barnacle")
outFile.WriteLine("Sitting in the tight place")
outFile.WriteLine("Laughing at the monkey arm")
outFile.WriteLine("Pulling like a china boy")
outFile.WriteLine("Carraway Carraway")
outFile.WriteLine("Carraway noise")
outFile.WriteLine("Boing si ka semala")
outFile.WriteLine("Boing si ka semala")
'close file
outFile.Close()
'Use new stream reader object to open file
inFile = New StreamReader(fileName)
'Read entire contents of file
inputText = inFile.ReadToEnd()
'Close file
inFile.Close()
'Display file contents in textbox
TextBox1.Text() &= "Testing Streams..." & vbCrLf
TextBox1.Text() &= inputText

3

Visual Basic

Lesson 22 – File IO & Streams

www.mrfraser.org

'Reomve the default text selection
TextBox1.Select(0, 0)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdQuit.Click
Application.Exit()
End Sub
Private Sub cmdOpenFile_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdTestFileIO.Click
TextBox1.Text() = ""
TestFileIO()
'call sub procedure TestFileIO
End Sub
Private Sub cmdTestStreams_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdTestStreams.Click
TextBox1.Text() = ""
TestStreams()
'call sub procedure TestStreams
End Sub
End Class

Extension Task:
Modify your program so that the user can input text to be written to a text file. The user should be
able to choose the write method, and specify a new name for the file. You should also modify your
code so that the user can choose which file to open, and which mode to open it with.
Hint: You will need to use an OpenFileDialog and a SaveFileDialog. You should pass the filename to
your different read / write methods that is returned from the Open/Save file dialogs.
Your form may look something like this:

4


22_FileIOandStreams.pdf - page 1/4
22_FileIOandStreams.pdf - page 2/4
22_FileIOandStreams.pdf - page 3/4
22_FileIOandStreams.pdf - page 4/4

Related documents


22 fileioandstreams
good shepherd ghana methodist church worcester
opgaveh fte c output
uspunit3
zd2911 user guide en
project report cpu monitor


Related keywords