Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Advertisements

Reading and Writing Text Files Svetlin Nakov Telerik Corporation
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
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.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Object-Oriented Application Development Using VB.NET 1 Chapter 13 Introduction to Data Access Classes and Persistence.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
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.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
1 Binary Files ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
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.
Using Arrays and File Handling
CIS 270—Application Development II Chapter 14—Files and Streams.
File I/O 11_file_processing.ppt
 2006 Pearson Education, Inc. All rights reserved Files and Streams.
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
Introduction to Exception Handling and Defensive Programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Object Oriented Software Development 10. Persistent Storage.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
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.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
 Pearson Education, Inc. All rights reserved Files and Streams.
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
CSC 298 Streams and files.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Ajay Tripathi Input Output. Ajay Tripathi Input/output (IO) refers to the operations for reading and writing data to streams and files. In the.NET Framework,
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.
Object-Oriented Application Development Using VB.NET 1 Chapter 13 Introduction to Data Access Classes and Persistence.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 6: Creating Windows–based Applications 1 Microsoft® Visual C# 2008.
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.
Files.
18 Files and Streams.
Lectures 12 Files & Streams Dr. Eng. Ibrahim El-Nahry.
How to work with files and data streams
Files and Streams.
Exception Handling.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
Topics Introduction to File Input and Output
MSIS 670: Object-Oriented Software Engineering
Files & Streams.
Chapter 3.5 Input and Output
Exception Handling.
Files and Streams Lect10 GC201 12/1/2015.
Fundaments of Game Design
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
How to work with files and data streams
Files and Streams.
Topics Introduction to File Input and Output
Presentation transcript:

Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I

Outline Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from a Sequential-Access File Serialization Using Dialogs File Exception Handling Case Study: A Transaction-Processing Program

Variables and arrays Only temporary Variable “goes out of scope” Program terminates Files Long term storage Persistent data in Secondary Storage Devices In this chapter, we explain how to create, update and process data files in Visual Basic programs. We will consider “sequential-access” files. Introduction

Data Hierarchy Bit (Binary Digit) Either zero or one. All data reduced to combinations of bits for processing. Byte Eight bits Character Two bytes Character set Set of all characters used to program and represent data on a particular computer (Digits, letters and special symbols) Data Hierarchy

Field Group of characters that convey a meaning. For example, a field consisting of uppercase and lowercase letters can represent a person’s name. Record Group of several, related fields. In a payroll system, for example, a record for a particular employee might include the following fields: (Employee ID, Name, Address, Hourly pay rate). File Group of related records Record key Identifies record to a particular entity. Sequential file Records stored in order of record-key. Data Hierarchy

To perform file processing in Visual Basic, namespace System.IO must be referenced. This namespace includes definitions for stream classes such as: FileStream: we use it to read data to, and write data from, sequential-access and random-access files. StreamReader : reads from a text file. StreamWriter: writes to a text file. BinaryReader: reads from a binary file. BinaryWriter: writes to a binary file. Files are opened by creating objects of these stream classes. Files and Streams

Using FileStream Class The FileStream class gives the user the capability to read from, write to, open, and close files on a file system. This class provides access to standard input and output files. As one of the most complete classes of file processing of the.NET Framework, FileStream is equipped with all necessary properties and methods. To use it, you must first declare a variable of it. Creating a Sequential-Access File

FileStream class has 9 constructors the one we will use the most is: FileStream(String, FileMode, FileAccess) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write permission. We use the members of FileAccess and FileMode Enumerations with the constructors of this class to create or open a file. Creating a Sequential-Access File

FileMode The members of the FileMode Enumerator are: FileMode.Append: If the file already exists, the new data will be added to its end. If the file doesn't exist, it will be created and the new data will be added to it FileMode.Create: If the file already exists, it will be deleted and a new file with the same name will be created. If the file doesn't exist, then it will be created FileMode.CreateNew: If the file already exists, the compiler will throw an error. If the file doesn't exist, it will be created FileMode.Open: If the file exists, it will be opened. If the file doesn't exist, an error would be thrown FileMode.OpenOrCreate: If the file already exists, it will be opened. If the file doesn't exist, it will be created FileMode.Truncate: If the file already exists, its contents will be deleted completely but the file will be kept, allowing you to write new data to it. If the file doesn't exist, an error would be thrown. Creating a Sequential-Access File

FileAccess In order to perform an operation on a file, you must specify to the operating system how to proceed. One of the options you have is to indicate the type of access that will be granted on the file. This access is specified using the FileAccess enumerator. The members of the FileAccess enumerator are: FileAccess.Write: New data can be written to the file. FileAccess.Read: Existing data can be read from the file. FileAccess.ReadWrite: Existing data can be read from the file and new data be written to the file. Creating a Sequential-Access File

After a file is opened it's FileStream object can be passed to a BinaryReader, BinaryWriter, StreamReader and StreamWriter classes to work with the data in the file. StreamReader is useful for reading lines of information from standard text file and StreamWriter is useful for writing to standard text files. BinaryWriter class is used to write binary data to a file. The BinaryReader is useful to us for reading the data from the BinaryFile. Creating a Sequential-Access File

Stream Writing: A streaming operation is typically used to create a stream. Once the stream is ready, you can write data to it. The writing operation is performed through various classes such as : StreamWriter, BinaryWriter. Stream Reading: As opposed to writing to a stream, you may want to read existing data from it. The Reading operation is performed through various classes such as StreamReader, BinaryReader. Creating a Sequential-Access File

Stream Closing: When you use a stream, it requests resources from the operating system and uses them while the stream is available. When you are not using the stream anymore, you should free the resources and make them available again to the operating system so that other services can use them. This is done by closing the stream. To close a stream, you can call the Close() method of the class(es) you were using. Before calling Close(), call the class' Flush() method to clear the memory areas that the object was using when performing its operations. Creating a Sequential-Access File

1. Declare FileStream object and create a text file named file.txt with access mode of writing Dim fs As New FileStream("file.txt", FileMode.Create, FileAccess.Write) 2. Create a new StreamWriter and pass the filestream object fs as argument Dim sw As New StreamWriter(fs) 3. Write text to the created file using the WriteLine or Write methods sw.WriteLine("This is an example of using file handling in VB.NET.") sw.WriteLine("This concept is interesting.") 4. Close the file sw.Close() 'closing the file fs.Close() ‘closing the stream StreamWriter Class

The default location where the files we create are saved is the bin directory of the Windows Application with which we are working. StreamWriter Class

StreamReader 1. Declare FileStream object to open the file named file.txt with access mode of reading Dim fs As New FileStream("file.txt", FileMode.Open, FileAccess.Read) 2. Create a new StreamReader and pass the filestream object fs as argument Dim sr As New StreamReader(fs) 3. Peek method of StreamReader object tells how much more data is left in the file While sr.Peek() > -1 'displaying text from text file in a RichTextBox for instance rtbFile.Text &= sr.ReadLine() End While 4. Close the file sr.Close() 'closing the file fs.Close() ‘closing the stream

Example – Working with sequential access file

Reading Data from a Sequential- Access File The StreamReader class supports several methods for reading text files and offers a way of determining whether you are at the end of the file: Line-by-line  using ReadLine method. An entire file  using ReadToEnd method. One character at a time  using Read method.

Reading Data from a Sequential- Access File Line-by-line We can read each line with a ReadLine method. To determine whether we have reached the end of the file, we call the Peek method of the StreamReader object. The Peek method reads the next character in the file without changing the place that we are currently reading. If we have reached the end of the file, Peek returns -1.

Reading Data from a Sequential- Access File An entire file: You can also read an entire text file from the current position to the end of the file by using the ReadToEnd method. One character at a time: If you need to read the file a character at a time, you can use the Read method. This method returns the integer character value of each character read. Following example demonstrates how to use the Read method:

Reading Data from a Sequential- Access File Dim intSingleChar as Integer Dim cSingleChar as String While sr.Peek <> -1 intSingleChar = sr.Read() ' Convert the integer value into a character cSingleChar = Chr(intSingleChar) End While

Files using File Dialogues

1 2

1 2 3

Exception Handling VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception object is created. The coding structure VB.NET uses to deal with such Exceptions is called the Try … Catch structure. In the coding area for your button, type the word Try. Then hit the return key on your keyboard. VB.NET completes the rest of the structure for you: Try Catch ex As Exception End Try

Exception Handling The Try word means “Try to execute this code”. The Catch word means “Catch any errors here”. The ex is a variable, and the type of variable is an Exception object. When you run your program, VB will Try to execute any code in the Try part. If everything goes well, then it skips the Catch part. However, if an error occurs, VB.NET jumps straight to Catch.

Exception Handling Dim x As Integer = TextBox1.Text Dim y As Integer = TextBox2.Text Dim z As Integer Try z = x / y MsgBox(z) Catch ex As Exception MsgBox(ex.Message) End Try