830 likes | 1.73k Views
C# - Files and Streams. Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access File. Files and Streams. When file opened C#: Creates an object Associates a stream with that object Three stream objects:
E N D
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access File
Files and Streams • When file opened C#: • Creates an object • Associates a stream with that object • Three stream objects: • Console.In: returns standard input stream object • Console.Out: returns standard output stream object • Console.Error: returns standard error stream object • Namespace System.IO needed for file processing
Files and Streams • System.IO.Stream: allows representation of stream as bits • FileStream: read to and write from sequential-access and random-access files • MemoryStream:transfer of data directly to and from memory • BufferedStream: uses buffer to transfer to memory
Classes File and Directory • Information stored in files • Files organized in directories • Directory class used to manipulate directories • File class used to manipulate files • Only has static methods, cannot instantiate File objects
Set fileName to what user typed See if fileName is an existing file Create StreamReader to read text from file Call method ReadToEnd // get user-specified file or directory string fileName = ??? // determine whether fileName is a file if ( File.Exists( fileName ) ) { // obtain reader and file contents try {StreamReader stream = new StreamReader( fileName ); string inputString = stream.ReadToEnd(); } // handle exception if StreamReader is unavailable catch( IOException ) { MessageBox.Show( "File Error", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } }
If user input is not existing file output error message FileTest.cs else { // notify user that neither file nor directory exists MessageBox.Show( filename + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } // end if
Instantiate SaveFileDialog object Show SaveFileDialog Test if user canceled save Get file name to save to // create dialog box enabling user to save file SaveFileDialog fileChooser = new SaveFileDialog(); DialogResult result = fileChooser.ShowDialog(); // allow user to create file fileChooser.CheckFileExists = false; // exit event handler if user clicked "Cancel“ if ( result != DialogResult.Cancel ) {// get specified file name string fileName = fileChooser.FileName; // show error if user specified invalid file if ( fileName == "" || fileName == null ) MessageBox.Show( "Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
Instantiate output stream with write permission Method to save data when user clicks enter else { // save file via FileStream if user specified valid file try { // open file with write access output = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Write ); } // notify user if file does not exist catch ( FileNotFoundException ) { MessageBox.Show( "File Does Not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } }
Creating or Writing to a Sequential-Access File • Programmers have to structure files to meet the requirements of applications • Declare and define a record class which describes the structure of the data • Write the main class to write out a record
Libraries Needed to read a file sequentially using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization;
Tells compiler objects of class Record can be represented as a set of bytes Data to go into record Sets members to 0 [Serializable] public class Record {private int account; private string firstName; private string lastName; private double balance; // set members to default values publicRecord() : this( 0, "", "", 0.0 ) { }
Set members to parameters // set members to parameter values publicRecord( int accountValue, string firstNameValue, string lastNameValue, double balanceValue ) { Account = accountValue; FirstName = firstNameValue; LastName = lastNameValue; Balance = balanceValue; } // end constructor
Other properties and methods Record.cs // property Account public int Account { get { return account; } set { account = value; } } // end property Account } //end class
Main class // Creating a sequential-access file of records // C# namespaces using System.IO; usingSystem.Runtime.Serialization.Formatters.Binary; usingSystem.Runtime.Serialization; // serializes Record in binary format private BinaryFormatter formatter = new BinaryFormatter(); // stream through which serializable data is written to file private FileStream output;
Store TextBox fields in record Write data to file // Record containing TextBox values to serialize Record record = new Record(); // store values in Record and serialize Record try { record.Account = Int32.Parse( values[ ( int )TextBoxIndices.ACCOUNT ] ); record.FirstName = values[ ( int )TextBoxIndices.FIRST ]; record.LastName = values[ ( int )TextBoxIndices.LAST ]; record.Balance = Double.Parse( values[ ( int )TextBoxIndices.BALANCE ] ); // write Record to FileStream (serialize object) formatter.Serialize( output, record ); }
Catch block if user input invalid data Close FileStream // notify user if error occurs in serialization catch( SerializationException ) { MessageBox.Show( "Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } // notify user if error occurs regarding parameter format catch( FormatException ) { MessageBox.Show( "Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } }
Don’t forget to close the output file! // invoked when user clicks Exit button private voidexitButton_Click( object sender, System.EventArgs e ) { // determine whether file exists if ( output != null ) { // close file try { output.Close(); }
Reading Data from a Sequential-Access File • Read data sequentially from a file • Programs usually start at beginning of file and read data consecutively until the desired data is found • Sometimes necessary to do this several times during execution of a program • File-position pointer: • Points to next byte to be read from or written to file • Can be repositioned to any point in file
User clicked open button Instantiate OpenFileDialog Show OpenFileDialog // invoked when user clicks Open File button private voidopenButton_Click( object sender, System.EventArgs e ) { // create dialog box enabling user to open file OpenFileDialog fileChooser = new OpenFileDialog(); DialogResult result = fileChooser.ShowDialog(); // exit event handler if user clicked Cancel if ( result != DialogResult.Cancel ) { // get name from user fileName = fileChooser.FileName; // show error if user specified invalid file if ( fileName == "" || fileName == null ) MessageBox.Show( "Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
87 88 // read and display file information 89 try 90 { 91 // close file from previous operation 92 if ( input != null ) 93 input.Close(); 94 95 // create FileStream to obtain read access to file 96input = newFileStream( fileName, FileMode.Open, FileAccess.Read );
While loop to read from file Read input from file 101 // traverse file until end of file 102while ( true ) 103 { 104 // get next Record available in file Record record = ( Record )reader.Deserialize( input ); // display record 114 string output = record.Account + "\t" + 115 record.FirstName + "\t" + record.LastName + 116 new string( ' ', 6 ) + "\t"; 117 118 // display balance with correct monetary format 119 output += String.Format( 120 "{0:F}", balance ) + "\r\n"; 121 122 // copy output to screen 123 displayTextBox.Text += output; 124 }
No more records exception Close FileStream // handle exception when no more records catch( SerializationException ) { // close FileStream if no Records in file input.Close(); }