file handling. data files programs that can only get data from the user interface are limited....

21
File Handling

Upload: kevin-booker

Post on 16-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

File Handling

Page 2: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Data Files

• Programs that can only get data from the user interface are limited.– data must be entered each time.– only small amounts of data can be processed.

• Both limitations can be circumvented by storing data in text files.

Page 3: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Data Files

• A text file is one that contains only ASCII characters.

• Operations that typically can be performed on files:– Read– Write– Append

Page 4: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Files as Objects

Dealing with files will require specifying their names and paths. To facilitate this the standard Windows Dialog boxes can be used.

The toolbox includes a group ofDialogs

Among these are:OpenFileDialog& SaveFileDialog

Page 5: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Including Dialog Controls

Like any other control, an OpenFileDialog must be added to the interface for a project to use it.

Simply click the icon in the toolbox and click on the form.

The control appears below the code, and cannot be resized because it does not appear in the interface.

Page 6: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

OpenFileDialog Properties

Like all controls, the OpenFileDialog has properties. Here are some examples:

FileName – perhaps the most important

Filter –specifies file types shown

InitialDirectory – specifies the initial directory

Page 7: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

OpenFileDialog Properties

For an OpenFileDialog Control called Test, a program could set properties at runtime like these…

dlgTest.Filter = “Music files|*.mp3"

dlgTest.InitialDirectory = "C:\My Music"

Page 8: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

OpenFileDialog Methods

The OpenFileDialog also has Methods.

One is of particular interest:– ShowDialog() As _System.Windows.Forms.DialogResult

Page 9: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

ShowDialog() Method

Note that ShowDialog() is a Function Method. It returns a result of type DialogResult, just like the MessageBox.

To call the function we simply assign its result to a variable of the appropriate type.

Dim showResult As DialogResult

showResult = dlgTest.ShowDialog()

Page 10: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

ShowDialog() Method

Once the ShowDialog() method has completed, the FileName property of the OpenFileDialog will be set to the file the user selected.

A project now needs to:• Open the file

– create a FileStream• Read the file

– create a StreamReader– use the StreamReader to read the data

Page 11: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Opening a File

Opening and reading a text file requires new classes:– IO.FileStream– IO.StreamReader

These classes, of course, have properties and methods. We need to declare objects of the classes, and then use methods on the objects to open and read the file.

Page 12: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Creating a FileStream

Create an object of the FileStream class:Dim theStream As IO.FileStream

Then initialise a new object of the class:

theStream = New _IO.FileStream(dlgTest.FileName, _IO.FileMode.Open, IO.FileAccess.Read)

Page 13: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Creating a StreamReader

Declare an object variable of the StreamReader class:

Dim theReader As IO.StreamReader

Create a new StreamReader object and associate it with the FileStream:

theReader = New IO.StreamReader(theStream)

Page 14: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Reading a File

StreamReader provides several ways to read data:

Read() As IntegerRead(ByVal buffer() As Char,_ByVal index As Integer,_ByVal count As Integer) As Integer

ReadBlock(ByVal buffer() As Char,_ByVal index As Integer,_ByVal count As Integer) As Integer

ReadLine() As StringReadToEnd() As String

Page 15: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Closing a File

Whichever method is used to read data, the file should always be Closed after use.

Close the StreamReader and the FileStream:

reader.Close()

theStream.Close()

Page 16: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Writing to a File

The SaveFileDialog has many of the same properties as OpenFileDialog.

Notice, however, that writing a file has a complication that reading doesn’t…

The FileName specified by the user may, or may not, exist. If it doesn’t exist,

• the user may want to Create a new file, OR • they may have mistyped a file they want to Overwrite.

Page 17: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Writing to a File

The CreatePrompt property determines if a message box is displayed to confirm creating a new file.

The OverwritePrompt property determines if a message box is displayed to confirm overwriting a file.

Page 18: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Writing to a File

The steps in using the SaveFileDialog are much the same as the OpenFileDialog:

1. show the SaveFileDialog window

2. create the FileStream and StreamWriter objects

3. write the text to the StreamWriter object

4. close the objects

Page 19: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Writing to a File

Dim theStream As IO.FileStreamDim writer As IO.StreamWriterDim SaveOK As DialogResultSaveOK = dlgSave.ShowDialog()theStream = New IO.FileStream_

(dlgSave.FileName,_IO.FileMode.Create,_IO.FileAccess.Write)

writer = New IO.StreamWriter(theStream)writer.Write(txtNewContent.Text)**

writer.Close()theStream.Close()

Page 20: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Writing to a File

** There are 35 different Methods for writing data to a file.

The example used writes a single string of text characters, followed by the eof character.

Page 21: File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of

Appending to a File

The third file operation is to Append text to the end of a text file.

The SaveFileDialog is used for all Write operations, but the FileStream must be created with the FileMode set to Append.

theStream = New _IO.FileStream(dlgSave.FileName, _IO.FileMode.Append, _IO.FileAccess.Write)