1 / 19

Chapter 18 – Files and Streams (p 644)

Chapter 18 – Files and Streams (p 644).

shiro
Download Presentation

Chapter 18 – Files and Streams (p 644)

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

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

  3. Classes File and Directory (p 647)

  4. Classes File and Directory (p 647)

  5. Demo Figure 18.5 Page 648

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

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

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

  9. Các Thành Viên Của Lớp StreamWriter

  10. Các Thành Viên Của Lớp StreamReader

  11. 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 );

  12. 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 );

  13. 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); }

  14. 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 );

  15. Creating a Sequential-Access File • Programmers have to structure files to meet the requirements of applications • DemoFigure 18.7 Page 656

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

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

  18. 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 );

  19. 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 ); }

More Related