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.

Slides:



Advertisements
Similar presentations
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Advertisements

Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: File and Stream Tujuan Instruksional Khusus: Mahasiswa dapat.
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Creating Sequential-Access File  Serializable attribute indicates to the compiler that objects of a class can be serialized– written to or read from a.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Sequential-access file Create, read and write an object into a sequential-access file Serialize and deserialize the object to write and read from a data.
File and Streams There are many ways of organizing records in a file. There are many ways of organizing records in a file. The most common type of organization.
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
CIS 270—Application Development II Chapter 14—Files and Streams.
 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
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
Lecture 19 Serialization Richard Gesick. Serialization Sometimes it is easier to read or write entire objects than to read and write individual fields.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.  persistent data.  The smallest.
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.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
 Pearson Education, Inc. All rights reserved Files and Streams.
 2009 Pearson Education, Inc. All rights reserved. 1 Ch.19 Files and Streams Many slides modified by Prof. L. Lilien (even many without explicit message).
Object Serialization. Sequential-access Text Files Sequential-access files store records In order by the record-key field Java imposes no structure on.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
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.
Input and Output 23: Input and Output
CSC 298 Streams and files.
Chapter 21 – Files and Streams 1 Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
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,
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
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.
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.
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.
TMF1414 Introduction to Programming
CSCI 3327 Visual Basic Chapter 11: Files and Streams
Files.
18 Files and Streams.
Lectures 12 Files & Streams Dr. Eng. Ibrahim El-Nahry.
CSCI 3327 Visual Basic Chapter 11: Files and Streams
Files and Streams.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
File Input/Output (I/O)
Topics Introduction to File Input and Output
MSIS 670: Object-Oriented Software Engineering
Files & Streams.
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides.
Files and Streams Lect10 GC201 12/1/2015.
Fundamental of Programming (C)
Fundaments of Game Design
Chapter 15 Files, Streams and Object Serialization
Files and Streams.
Topics Introduction to File Input and Output
Presentation transcript:

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. Data maintained in files often is called persistent data. The smallest data item that computers support is called a bit (short for “binary digit”—a digit that can assume one of two values). Digits, letters and special symbols are referred to as characters. Bytes are composed of eight bits. C# uses the Unicode ® character set ( in which characters are composed of 2 bytes. Just as characters are composed of bits, fields are composed of characters. A field is a group of characters that conveys meaning. Typically, a record is composed of several related fields. A file is a group of related records. To facilitate the retrieval of specific records from a file, at least one field in each record is chosen as a record key, which uniquely identifies a record. A common file organization is called a sequential file, in which records typically are stored in order by a record-key field.

Data

Files and Streams C# views each file as a sequential stream of bytes When a console application executes, the runtime environment creates the Console.Out, Console.In and Console.Error streams. Console.In refers to the standard input stream object, which enables a program to input data from the keyboard. Console.Out refers to the standard output stream object, which enables a program to output data to the screen. Console.Error refers to the standard error stream object, which enables a program to output error messages to the screen. Console methods Write and WriteLine use Console.Out to perform output Console methods Read and ReadLine use Console.In to perform input.

System.IO The System.IO namespace includes stream classes such as StreamReader, StreamWriter and FileStream for file input and output. These stream classes inherit from abstract classes TextReader, TextWriter and Stream, respectively. Abstract class Stream provides functionality for representing streams as bytes. Classes FileStream, MemoryStream and BufferedStream (all from namespace System.IO) inherit from class Stream. Class FileStream can be used to write data to and read data from files.

Class File File ’s static methods for manipulating and determining information about files:

Class File ’s static methods

Class Directory Class Directory provides capabilities for manipulating directories.

Class Directory The DirectoryInfo object returned by method CreateDirectory contains information about a directory.

FileFormTest example Let’s look at an example to get information about files and directories, list files in a directory or display contents of a file. The StreamReader constructor takes as an argument a string containing the name of the file to open. StreamReader method ReadToEnd read the entire contents of the file as a string. Call Directory method GetDirectories to obtain an array of subdirectories in the specified directory.

Records C# imposes no structure on files. Thus, the concept of a “record” does not exist in C# files. Our examples will have a bank account as a record. The record contains Account number, First and Last Names and the Balance of the account. See Record.cs

Common Input UI: BankUIForm

Create an Output File Let’s look at our example of writing accounts into a file. CreateFileForm solution. CreateFileForm inherits from BankUIForm CreateFileForm uses instances of class Record to create a sequential-access file.

CreateFileForm Class SaveFileDialog is used for selecting files. The constant FileMode.OpenOrCreate indicates that the FileStream should open the file if it exists or create the file if it does not. To preserve the original contents of a file, use FileMode.Append. The constant FileAccess.Write indicates that the program can perform only write operations with the FileStream object. There are two other FileAccess constants— FileAccess.Read for read-only access and FileAccess.ReadWrite for both read and write access. An IOException is thrown if there is a problem opening the file or creating the StreamWriter. StreamWriter method WriteLine writes a sequence of characters to a file. The StreamWriter object is constructed with a FileStream argument that specifies the file to which the StreamWriter will output text. Method Close throws an IOException if the file or stream cannot be closed properly.

Reading Files Now we’re going to read records from the file created by the CreateFileForm program, then display the contents of each record. Solution: ReadSequentialAccessFileForm

ReadSequentialAccessFileForm OpenFileDialog is used to open a file. The behavior and GUI for the Save and Open dialog types are identical, except that Save is replaced by Open. Specify read-only access to a file by passing constant FileAccess.Read as the third argument to the FileStream constructor. StreamReader method ReadLine reads the next line from the file.

File Position A FileStream object can reposition its file-position pointer to any position in the file. When a FileStream object is opened, its file-position pointer is set to byte position 0. You can use StreamReader property BaseStream to invoke the Seek method of the underlying FileStream to reset the file-position pointer back to the beginning of the file. Exercise: Add a Start/Beginning button to the ReadSequentialAccessFileForm program to go to the very first record.

Serialization Sometimes it is easier to read or write entire objects than to read and write individual fields. C# provides such a mechanism, called object serialization. A serialized object is an object represented as a sequence of bytes that includes the object’s data, its type and the types of data stored in the object. After a serialized object has been written to a file, it can be read from the file and deserialized.

Serialization Class BinaryFormatter enables entire objects to be written to or read from a stream. BinaryFormatter method Serialize writes an object’s representation to a file. BinaryFormatter method Deserialize reads this representation from a file and reconstructs the original object. Both methods throw a SerializationException if an error occurs during serialization or deserialization. Example: Serialization solution

Serialization (writing to a file) Method Serialize takes the FileStream object as the first argument so that the BinaryFormatter can write its second argument to the correct file. Remember that we are now using binary files, which are not human readable.

Deserialization (reading from a file) Example: Deserialization solution Deserialize returns a reference of type object. If an error occurs during deserialization, a SerializationException is thrown.