1 / 21

File Handling

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 can be processed. Both limitations can be circumvented by storing data in text files. Data Files.

willem
Download Presentation

File Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Handling

  2. 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.

  3. Data Files • A text file is one that contains only ASCII characters. • Operations that typically can be performed on files: • Read • Write • Append

  4. 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

  5. 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.

  6. 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

  7. 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"

  8. OpenFileDialog Methods The OpenFileDialogalso has Methods. One is of particular interest: • ShowDialog() As _System.Windows.Forms.DialogResult

  9. 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()

  10. 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

  11. 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.

  12. 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)

  13. 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)

  14. Reading a File StreamReader provides several ways to read data: Read() As Integer Read(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 String ReadToEnd() As String

  15. 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()

  16. 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.

  17. 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.

  18. Writing to a File The steps in using the SaveFileDialog are much the same as the OpenFileDialog: • show the SaveFileDialog window • create the FileStream and StreamWriter objects • write the text to the StreamWriter object • close the objects

  19. Writing to a File Dim theStream As IO.FileStream Dim writer As IO.StreamWriter Dim SaveOK As DialogResult SaveOK = 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()

  20. 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.

  21. 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 theFileStream must be created with the FileMode set to Append. theStream = New _IO.FileStream(dlgSave.FileName, _IO.FileMode.Append, _IO.FileAccess.Write)

More Related