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.

Slides:



Advertisements
Similar presentations
Files, Printing, and Structures
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Copyright © 2012 Pearson Education, Inc. Chapter 11 MORE WINDOWS CONTROLS & STANDARD DIALOG BOXES.
Chapter 7: Sub and Function Procedures
File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
Dialog Boxes. Slide 2 The OpenFileDialog and SaveFileDialog Controls All dialog boxes derive from the CommonDialog class and share similar features The.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
1 Prototyping for HCI Spring 2004 (Week 7) Jorge A. Toro.
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Input/Output CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Visual C Sharp – File I/O - 1 Variables and arrays only temporary - lost when a program terminates Files used for long term storage (Data bases considered.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
File I/O 11_file_processing.ppt
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
BIL528 – Bilgisayar Programlama II Introduction 1.
Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
The Professional Touch Programming Right from the Start with Visual Basic.NET 1/e 10.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Reading & writing to files
Files.
File Handling.
Using Multiple Forms.
Ch 10 Sequential Access Files
Files and Streams.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
File Input/Output (I/O)
Multi-form applications and dialogs
Chapter 3.5 Input and Output
Input and Output.
Control Structures Part B - Message and Input Boxes
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
Dictionary Builder Part 4 Loading Files.
CIS16 Application Development and Programming using Visual Basic.net
Part 2 Saving the Dictionary
Input and Output.
Files and Streams.
Chapter 11 Saving Data and Objects In Files
Input and Output Chapter 3.5
Presentation transcript:

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 A text file is one that contains only ASCII characters. Operations that typically can be performed on files: –Read –Write –Append

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 of Dialogs Among these are: OpenFileDialog & SaveFileDialog

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.

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

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"

OpenFileDialog Methods The OpenFileDialog also has Methods. One is of particular interest: –ShowDialog() As _ System.Windows.Forms.DialogResult

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

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

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.

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)

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)

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

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

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.

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.

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

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

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.

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)