190 likes | 274 Views
Chapter 18 – Files and Streams (p 644).
E N D
Chapter 18 – Files and Streams (p 644) Outline18.1 Introduction18.2 Data Hierarchy18.3 Files and Streams18.4 Classes File and Directory18.5 Creating a Sequential-Access File18.6 Reading Data from a Sequential-Access File18.7 Random-Access Files18.8 Creating a Random-Access File18.9 Writing Data Randomly to a Random-Access File18.10 Reading Data Sequentially from a Random-Access File18.11 Case Study: A Transaction-Processing Program
Introduction (p 644) • Variables and arrays only temporary • Lost during garbage collection or when a program terminates • Files used for long term storage • Called persistent data • This chapter deals with: • Sequential-access files • Random-access files • File processing features • Stream-input/output features
Demo Figure 18.5 Page 648
Data Hierarchy • Data Hierarchy: • Gets more complex as you move along: • Bit: either one or zero • All data represented as combination of bits • Easy for electronic devices to understand • Byte: eight bits • Character: in C# two bytes • Character set: set of all characters used to program and represent data on a particular computer • Field: composition of characters that convey a meaning • Record: composition of several, related fields • File: group of related records • Record key: identifies record to a particular entity • Sequential file: records stored in order of record-key
Sally Black Tom Blue Judy Judy Green Green file Iris Orange Randy Red record Judy Field 01001010 byte (ASCII for J) 1 bit Data Hierarchy (p 645) Fig. 17.1 Data hierarchy.
Access Text File LÀM VIỆC VỚI NHỮNG TẬP TIN VĂN BẢN • Để làm việc với tập tin văn bản (đọc/ghi) ta cần sử dụng đến các lớp StreamReader và StreamWriter. • Cả 2 lớp này, theo mặt định đều làm việc với ký tự Unicode.
Sequential-Access Text File • Đọc và viết dữ liệu thông qua lớp Stream. Stream nghĩa là dòng dữ liệu chảy đi. • Lớp FileStream: cho phép mở những tập tin đã có cũng như tạo mới tập tin. • FileStream thường dùng những enum FileMode, FileAccess, FileShare. FileStream output = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Write );
Creating and writting a Sequential-Access File (p 659) Figure 18.9 (TẠO VÀ GHI FILE) 1. using System.IO; 2. // writes data to text file private StreamWriter fileWriter; // maintains connection to file private FileStream output; 3. // open file with write access output = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Write ); // sets file to where data is written fileWriter = new StreamWriter( output );
Creating and writting a Sequential-Access File (p 661) if (accountNumber > 0) { record.Account = accountNumber; record.FirstName = txtFirstName.Text; record.LastName = txtLastName.Text; record.Balance = Convert.ToDecimal(txtBalance.Text); // write Record to file, fields separated by commas fileWrite.WriteLine(record.Account + " ," + record.FirstName + " ," + record.LastName + " ," + record.Balance); }
Reading sequential-access files • ĐỌC FILE: private FileStream input; private StreamReader fileReader; // create FileStream input = new FileStream( fileName, FileMode.Open, FileAccess.Read ); // set file from where data is read fileReader = new StreamReader( input );
Creating a Sequential-Access File • Programmers have to structure files to meet the requirements of applications • DemoFigure 18.7 Page 656
Serialization (p 678) • System.Runtime.Serialization.Formatters.Binary • Class BinaryFormatter • Serialize (lưu trữ dữ liệu) • Deserialize (tái tạo dữ liệu) • SerializationException
Creating a Sequential-Access File Using Object Serialization (p 679) • Marked with the [Serializable] attribute to the CLR that objects of class Record can be serialized
Using a Serialization Stream to Create an Output File (p 680) 1. using system.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; 2. private BinaryFormatter formatter = new BinaryFormatter(); private FileStream output; 3. output = new FileStream( fileName , FileMode.OpenOrCreate, FileAccess.Write );
Using a Serialization Stream to Create an Output File (p 682) if (accountNumber > 0) { record.Account = accountNumber; record.FirstName = txtFirstName.Text; record.LastName = txtLastName.Text; record.Balance = Convert.ToDecimal(txtBalance.Text); // write Record to file, fields separated by commas fileWrite.WriteLine(record.Account + " ," + record.FirstName + " ," + record.LastName + " ," + record.Balance); // write Record to FileStream ( serialize object ) formatter.Serialize( output, record ); }