1 / 21

Files and Streams

Files and Streams. Ch 17. Payroll Example. Persistent Data. Data maintained in files Hierarchical file system A PC contains one or more disks A disk contains 0 or many folders (directories) A folder contains 0 or many files, and 0 or many subfolders. Data Hierarchy.

elebron
Download Presentation

Files and Streams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files and Streams Ch 17

  2. Payroll Example

  3. Persistent Data • Data maintained in files • Hierarchical file system • A PC contains one or more disks • A disk contains 0 or many folders (directories) • A folder contains 0 or many files, and 0 or many subfolders

  4. Data Hierarchy • The smallest data item is a bit • Characters are composed of bits • A field is a group of characters • A record is composed of several fields • A file can be used to store several records

  5. Files and Streams • C# views each file as a sequential stream of bytes • When a file is opened, • an object is created and a stream is associated with the object

  6. console Streams • Console.In • Standard input stream object • Enables a program to input data from the keyboard • Console.Out • Standard output stream object • Enables a program to output data to the screen • Console.Error • Standard error stream object • Enables a program to output error messages to the screen

  7. File-processing Classes • Class StreamReader • For text input from a file • Class StreamWriter • For text output to a file • Class FileStream • For both input from and output to a file • Class MemoryStream • Enables the transfer of data directly to and from memory • Class BufferedStream • Uses buffering to transfer data to or from a stream

  8. File Open and Save Dialogs • Help specify a filename and path • OpenFileDialog • If the file should already exist • SaveFileDialog • Specify a filename and path for a new file

  9. Sequential-Access File • Data in file are structured into records • Records are read (or written) one at a time, from the start to the end

  10. Serialization • A serialized object is represented as a sequence of bytes, including the object’s: • Data • Type • The types of data stored in the object • Entire serialized object can be written to and read from a file

  11. Class BinaryFormatter • Enables entire objects to be written to or read from a stream • Method Serialize • Writes an object’s representation to a file • Method Deserialize • Reads this representation from a file and reconstructs the original object • Both methods require a Stream object as a parameter • So that the BinaryFormatter can access the correct stream

  12. Class for Serializable Objects • Must include[Serializable] attribute or implement interface ISerializable • Indicates that objects of class can be serialized • Must ensure every instance variable of the class is also serializable • All simple-type variables and strings are serializable

  13. Serializable Class Example [Serializable] publicclassAddress [Serializable] publicclassPhoneNumber [Serializable] publicabstractclassEmployee [Serializable] publicclassSalariedEmployee : Employee

  14. Writing Data to a File

  15. Serializing Objects using System; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; namespace Payroll { publicpartialclassPayrollSystemForm : Form {

  16. privatevoid saveToolStripMenuItem_Click(object sender, EventArgs e) { BinaryFormatter formatter = newBinaryFormatter(); FileStream output = null; DialogResult result; string fileName; // name of file to save data using (SaveFileDialog fileChooser = newSaveFileDialog()) { // allow user to create file fileChooser.CheckFileExists = false; result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; }

  17. if (result == DialogResult.Cancel) return; try { // open file with write access output = newFileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); // save records to file foreach (Employee item in lstEmployees.Items) { formatter.Serialize(output, item); } } // end try // handle exception if there is a problem catch (IOException)

  18. Reading Data From the File

  19. privatevoid openToolStripMenuItem_Click(object sender, EventArgs e) { BinaryFormatter reader = newBinaryFormatter(); FileStream input = null; DialogResult result; string fileName; // name of file using (OpenFileDialog fileChooser = newOpenFileDialog()) { result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; // get file name } if (result == DialogResult.Cancel) return;

  20. try { input = newFileStream(fileName, FileMode.Open, FileAccess.Read); // read records from file lstEmployees.Items.Clear(); Employee employee = (Employee)reader.Deserialize(input); while (employee != null) { lstEmployees.Items.Add(employee); employee = (Employee)reader.Deserialize(input); } } catch

  21. Summary • Persistent data • Data maintained in files • C# views each file as a sequential stream of bytes • Sequential-access file • Records in file read/written from start to end • Serialization • Allows entire serialized objects to be written to and read from streams

More Related