File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.

Slides:



Advertisements
Similar presentations
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Advertisements

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.
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
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.
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
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
Chapter 9 Files I/O: Files, Records and Fields Part 3.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
File I/O 11_file_processing.ppt
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
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.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.
Applications Development
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.
CS360 Windows Programming
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
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.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
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.
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
Reading & writing to files
Files.
Files and Streams.
Files and Streams Lect3 CT1411.
File Input/Output (I/O)
Visual Basic..
Topics Introduction to File Input and Output
Chapter 3.5 Input and Output
Input and Output.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
Tonga Institute of Higher Education
CIS16 Application Development and Programming using Visual Basic.net
BO65: PROGRAMMING WRITING TO TEXT FILES.
Input and Output.
Files and Streams.
Topics Introduction to File Input and Output
Chapter 11 Saving Data and Objects In Files
Input and Output Chapter 3.5
Presentation transcript:

File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O

Visual Basic Techniques for File I/O Only Available in Visual Basic, not other.NET languages such as C# or JavaScript. Good for writing a single string No need to open or close files. General Forms: Unless specified, files are created in bin\Debug of project. My.Computer.FileSystem.WriteAllText(FileName, StringToWrite, AppendBoolean) My.Computer.FileSystem.ReadAllText(Fileame)

Some Simple Examples This example appends Rick Bournique to the end of the names.txt file. This example tries to open Names.txt and read the data in the file into the variable Name. Try Name = My.Computer.FileSystem.ReadAllText( "Names.txt") Catch MessageBox.Show("File or path not found or invalid") End Try My.Computer.FileSystem.WriteAllText("Names.txt", “Rick Bournique, True)

Writing & Reading Delimited Files You can write multiple fields into a file by separating the fields with a delimiter- a character that separates the fields. E.g., Jack, Jill, Reading requires a TextFieldParser object: My.Computer.FileSystem.WriteAllText("Names.txt", StudentName & "," & StudentPhone & Environment.NewLine, True) Imports Microsoft,VisualBasic.FileIO Private NamePhoneParser As TextFieldParser NamePhoneParser = New TextFieldParser(FileString) NamePhoneParser.TextFieldType = FieldType.Delimited NamePhoneParser.SetDelimiters(“,”)

Reading Fields with TextFieldParser Once you’ve set up a TextFieldParser object, use the ReadFields() method to read the fields into an array you created: Dim TheName, ThePhone As String Dim FieldsArray() As String = NamePhoneParser.ReadFields() TheName = FieldsArray(0) ThePhone = FieldsArray(1)

File Handling Using Streams A stream is the transfer of a series of bytes from one location to another. Streams are found in the System.IO namespace. Use an Imports statement. Compared to the VB techniques, streams provide more robust and universal file handling in the.NET framework.

Steps for Writing Data in a File Using Streams 1. Declare a new StreamWriter object. 2. Use the StreamWriter 's WriteLine() method to copy data to a buffer in memory. 3. Call StreamWriter 's Close() method to transfer data from buffer to the file and release system resources used by the stream.

Step 1: Instantiating a StreamWriter Object General Forms: Default location for file is bin/Debug folder beneath the folder of the current project. BooleanAppend lets you append data to an existing file rather than overwrite the existing file. Set to True to append. Dim ObjectName As New StreamWriter("FileName") Dim ObjectName As New StreamWriter("FileName", BooleanAppend) Dim MyStreamWriter As New StreamWriter("Phone.txt“)

Step 2: Write the Data Write() method places items consecutively in the file with no delimiter (separator) WriteLine() method places an enter (carriage return) between the items General Form: DataToWrite argument may be string or numeric data. Any numeric data is converted to a string. Examples: StreamObjectName.WriteLine(DataToWrite) NameStreamWriter.WriteLine(“Rick Bournique”) IncomeStreamWriter.Write(50000)

Step 3: Close the Stream Use the StreamWriter 's Close() method. Close() finishes writing all data from the stream's buffer to the disk and releases system resources. Commonly inserted in the form’s closing event procedure. If the file is not closed, it may remain open for an indefinite time and sometimes may become unusable. Example: Private Sub ExitButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ExitButton.Click PhoneStreamWriter.Close() Me.Close() End Sub

Steps for Reading Data from a File Using Streams 1. Declare a new StreamReader object. This opens the file for reading. 2. Use the StreamWriter 's ReadLine() method to get the data from the stream. Typically this statement is in a loop to get multiple records. 3. Call StreamReader 's Close() method to release system resources used by the stream.

Step 1: Instantiating a StreamReader Object Declare the StreamReader object in a procedure and enclose it in a Try/Catch block. The file must exist in the specified location where the application expects it or an exception occurs. Try Dim MyStreamReader As New StreamReader(“who.txt“) Catch MessageBox.Show(“File does not exist.”) End Try

Step 2: Read the Data The ReadLine() method is used to read previously saved lines of data from the file. Typically this is in a loop to read multiple lines. Assign the value from the read to the desired location, such as a label, text box, or string variable. Example Note that ReadLine() has no arguments. PhoneTextBox.Text = PhoneStreamReader.ReadLine()

Checking for the End of the File Use StreamReader 's. Peek() method to look at the next element without reading it. If you Peek() beyond the last element, the value returned is -1. Example: The discussion of Peek() in the text and the example programs (starting on page 448) is full of errors! Peek() is a method and therefore requires () when used. If MyStreamReader.Peek() <> -1 Name.TextBox = MyStreamReader.ReadLine() End If

Step 3: Close the Stream The Close() method is used to terminate the stream. Example: Use the VB keyword Nothing to check for instantiation of the stream reader. Syntax is important: use the keyword IsNot rather than <> If MyStreamReader IsNot Nothing MyStreamReader.Close() End If