Chương 8 . Files và Streams potx

105 914 4
Chương 8 . Files và Streams potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chương 8 . Files Streams  Outline 8.1. Files Streams 8.2. Lớp File lớp Directory 8.3. File truy xuất tuần tự 8.4. File truy xuất ngẫu nhiên 8.1. Files Streams Mục đích của nghiên cứu phần này:  Cung cấp khả năng khởi tạo, đọc, viết khả năng cập nhật File.  Hiểu được luồng thông tin (Stream) trong C#.  Có thể sử dụng lớp File thư mục.  Có thể sử dụng được các lớp FileStream lớp BinaryFormatter để đọc viết các đối tượng vào trong các File.  Nắm vững việc xử lý các File truy xuất tuần tự File truy xuất ngẫu nhiên.  File được sử dụng như bộ nhớ ngoài để lưu trữ với số lượng lớn dữ liệu,và có thể giữ lại dữ liệu thậm chí sau khi chương trình kết thúc.  Mỗi File kết thúc với một kí tự đánh dấu kết thúc File hoặc một số Byte xác định được ghi trong hệ thống lưu trữ quản lý cấu trúc dữ liệu. Files ? Phần tử đầu tiên của File Phần tử cuối (thứ n) của File Dấu hiệu kết thúc File Cấu tạo của File trong lưu trữ Stream ?  Stream (luồng) là luồng của thông tin, chứa thông tin sẽ được chuyển qua, còn tập tin thì để lưu trữ thông tin. Khi một File được mở ra:  C# sẽ tạo một đối tượng.  Nối luồng thông tin với đối tượng này. Có 3 đối tượng stream:  Console.In : trả về một đối tượng stream vào chuẩn.  Console.Out: trả về một đối tượng stream ra chuẩn.  Console.Error: trả về một đối tượng stream thông báo lỗi chuẩn. 8.1. Files Streams BinaryFormatter sử dụng 2 phương thức Serialize Deserialize để viết đọc đối tượng từ trong luồng:  Serialize:chuyển đổi một đối tượng sang một định dạng, có thể được viết vào File mà không mất dữ liệu.  Deserialize: đọc dữ liệu đã định dạng từ một File chuyển nó về dạng ban đầu. System.IO.Stream cho phép thể hiện dưới dạng bit của stream:  FileStream: đọc viết từ File truy xuất trình tự ngẫu nhiên:  MemoryStream: chuyển đổi dữ liệu trực tiếp với bộ nhớ.  BufferedStream: sử dụng bộ nhớ đệm để chuyển dữ liệu. 8.1. Files Streams 8.2. Lớp File lớp Directory  Thông tin được lưu trữ trên các files  Files được tổ chức thành các thư mục  Lớp Directory dùng để thao tác các thư mục  Lớp File dùng để thao tác trên các files  Chỉ có phương thức tĩnh,không thể khởi tạo đối tượng File 8.2. Lớp File lớp Directory 8.2. Lớp File lớp Directory [...] .. . System.Windows.Forms; System.Data; BankUI.cs public class BankUIForm : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public System.Windows.Forms.Label accountLabel; public System.Windows.Forms.TextBox accountTextBox; public System.Windows.Forms.Label firstNameLabel; public System.Windows.Forms.TextBox firstNameTextBox; public System.Windows.Forms.Label lastNameLabel ;.. . MessageBox.Show( "Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); CreateSequentialAccessFile.cs 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 else { // save file via FileStream if user specified valid file try { // open file with write access output = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Write ) ;.. . của thư mục fileName FileTest.cs 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 // get directory's creation date, // modification date, etc outputTextBox.Text = GetInformation( fileName ); // obtain file/directory list of specified directory directoryList = Directory.GetDirectories( fileName ); outputTextBox.Text += "\r\n\r\nDirectory contents:\r\n" ;.. . strng) myControl.Text = ""; } } } // end method ClearTextBoxes để xoá 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 // set text box values to string array values Phương thức để public void SetTextBoxValues( string[] values ) thiết lập giá trị của BankUI.cs { // determine whether string array has correct length textboxes if ( values.Length != TextBoxCount .. . 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // FileTest.cs // Using classes File and Directory FileTest.cs using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; // displays contents of files and directories public class FileTestForm : System.Windows.Forms.Form {.. . CreateSequentialAccessFile.cs // Creating a sequential-access file // C# using using using using using using using using using namespaces System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.IO; System.Runtime.Serialization.Formatters.Binary; System.Runtime.Serialization; // Deitel namespace using BankLibrary; public class CreateFileForm : BankUIForm { private System.Windows.Forms.Button .. . 48 49 50 51 52 53 54 55 56 57 58 59 // property Account public int Account { get { return account; } set { account = value; } } // end property Account // property FirstName public string FirstName { get { return firstName; } set { firstName = value; } } // end property FirstName Record.cs Các phương thức truy cập 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 .. . ghi vào File Ghi dữ liệu vào File Phương thức để ghi dữ liệu vào File Nhớ là phải đóng File vào Tạo khả năng truy cập File(viết vào File) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // BankUI.cs // A reusable windows form for the examples in this chapter using using using using using using System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms ;.. . FileTestForm : System.Windows.Forms.Form { private System.Windows.Forms.Label directionsLabel; private System.Windows.Forms.TextBox outputTextBox; private System.Windows.Forms.TextBox inputTextBox; private System.ComponentModel.Container components = null; Textbox để nhập đường dẫn file thư mục [STAThread] static void Main() { Application.Run( new FileTestForm() ); } // Visual Studio NET generated .. . System.Windows.Forms.Button saveButton; private System.Windows.Forms.Button enterButton; private System.Windows.Forms.Button exitButton; private System.ComponentModel.Container components = null; // serializes Record in binary format private BinaryFormatter formatter = new BinaryFormatter(); // stream through which serializable data is written to file private FileStream output; CreateSequentialAccessFile.cs . Chương 8 . Files và Streams  Outline 8. 1. Files và Streams 8. 2. Lớp File và lớp Directory 8. 3. File truy xuất tuần tự 8. 4. File truy xuất ngẫu nhiên 8. 1. Files và Streams Mục. BufferedStream: sử dụng bộ nhớ đệm để chuyển dữ liệu. 8. 1. Files và Streams 8. 2. Lớp File và lớp Directory  Thông tin được lưu trữ trên các files  Files được tổ chức thành các thư mục  Lớp Directory. " "; 83 } 84 else 85 { 86 // notify user that neither file nor directory exists 87 MessageBox.Show( inputTextBox.Text + 88 " does not exist", "File Error", 89 MessageBoxButtons.OK,

Ngày đăng: 25/03/2014, 12:21

Từ khóa liên quan

Mục lục

  • Chương 8 . Files và Streams

  • 8.1. Files và Streams

  • Files ?

  • Cấu tạo của File trong lưu trữ

  • Stream ?

  • Slide 6

  • Slide 7

  • 8.2. Lớp File và lớp Directory

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • FileTest.cs Program Output

  • 8.3. File truy xuất tuần tự

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan