Files and Streams - Programming Using C Sharp - Lecture Slides, Slides of Advanced Algorithms

The key points in the Advanced Algorithms are:Files and Streams, Binary Digit, Persistent Data, Character Set, Record, Related Records, Record Key, Sequential File, Data, Sequential Stream

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

102 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Files & Streams
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Files and Streams - Programming Using C Sharp - Lecture Slides and more Slides Advanced Algorithms in PDF only on Docsity!

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 (www.unicode.org) 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.

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’s static methods

static Method Description

GetLastWriteTime (^) Returns a DateTime object representing when the file was last modified. Move (^) Moves the specified file to a specified location. Open (^) Returns a FileStream associated with the specified file and equipped with the specified read/write permissions. OpenRead (^) Returns a read-only FileStream associated with the specified file. OpenText (^) Returns a StreamReader associated with the specified file. OpenWrite (^) Returns a read/write FileStream associated with the specified file.

Class Directory

  • Class Directory provides capabilities for manipulating directories.

static Method Description CreateDirectory (^) Creates a directory and returns its associated DirectoryInfo object. Delete (^) Deletes the specified directory. Exists (^) Returns true if the specified directory exists and false otherwise. GetDirectories (^) Returns a string array containing the names of the subdirectories in the specified directory. GetFiles (^) Returns a string array containing the names of the files in the specified 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

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.

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

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