Input/Output with .NET Framework Class Library pps

45 140 0
Input/Output with .NET Framework Class Library pps

Đ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

Input/Output with NET Framework Class Library Phạm Minh Tuấn Bộ mơn CNPM – Khoa CNTT Nội dung trình bày    Navigating the File System Reading and Writing Files Compressing Streams Navigating the File System  Nhu cầu:      Làm biết hệ thống có ổ đĩa nào? Làm lấy danh sách tập tin thư mục thư mục đó? Làm truy xuất thuộc tính tập tin, thư mục? Làm giám sát thay đổi tập tin, thư mục? … Navigating the File System  Các lớp hỗ trợ        DriveInfo class DirectoryInfo class FileInfo class Path class FileSystemWatcher class File class Directory class Navigating the File System  DriveInfo class  Thuộc tính Navigating the File System  DriveInfo class  DriveType enum  Phương thức Navigating the File System  FileSystemInfo class  Thuộc tính Navigating the File System  FileSystemInfo class  Phương thức Navigating the File System  DirectoryInfo class  Thuộc tính Navigating the File System  DirectoryInfo class  Phương thức Reading and Writing Files  Ví dụ Ghi file văn FileStream theFile = File.Open(@"c:\somefile.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter writer = new StreamWriter(theFile); writer.WriteLine("Hello"); writer.Close(); theFile.Close();  Reading and Writing Files  BinaryReader class   Dùng để đọc file nhị phân Cung cấp phương thức có dạng ReadXXX(ReadByte, ReadInt32…) để đọc nội dung file Reading and Writing Files  Ví dụ Đọc file nhị phân FileStream theFile = File.Open(@"c:\somefile.bin", FileMode.Open); BinaryReader reader = new BinaryReader(theFile); long number = reader.ReadInt64(); byte[] bytes = reader.ReadBytes(4); string s = reader.ReadString();  Reading and Writing Files  BinaryWriter class   Dùng để ghi file nhị phân Cung cấp phương thức Write với nhiều dạng tham số khác để ghi nội dung file Reading and Writing Files  Ví dụ  Ghi file nhị phân FileStream theFile = File.Open(@"c:\somefile.bin", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter writer = new BinaryWriter(theFile); long number = 100; byte[] bytes = new byte[] { 10, 20, 50, 100 }; string s = “Toi di hoc"; writer.Write(number); writer.Write(bytes); writer.Write(s); Reading and Writing Files  MemoryStream class:  Thuộc tính  Phương thức kế thừa Stream class Reading and Writing Files  Ví dụ MemoryStream memStrm = new MemoryStream(); StreamWriter writer = new StreamWriter(memStrm); writer.WriteLine("Hello"); writer.WriteLine("Goodbye"); writer.Flush(); FileStream theFile = File.Create(@"c:\inmemory.txt"); memStrm.WriteTo(theFile); Reading and Writing Files  BufferedStream class   Thường dùng cho NetworkStream Tăng hiệu đọc/ghi liệu FileStream newFile = File.Create(@"c:\test.txt"); BufferedStream buffered = new BufferedStream(newFile); StreamWriter writer = new StreamWriter(buffered); writer.WriteLine("Some data"); Compressing Streams    Cho phép nén liệu gốc tối đa 4GB Kiểu nén: GZIP DEFLATE Các lớp hỗ trợ   GZipStream class DeflateStream class Compressing Streams  GZipStream class Kế thừa Stream class  Dùng để nén/giải nén tập tin theo GZIP  Nén tập tin FileStream sourceFile = File.OpenRead(inFilename); FileStream destFile = File.Create(outFilename); GZipStream compStream = new GZipStream(sourceFile, CompressionMode.Compress); byte[] Arr = new byte[4096]; int theByte = sourceFile.Read(Arr, 0, 4096); while(theByte!=0) { compStream.Write(Arr, 0, theByte); theByte = sourceFile.Read(Arr, 0, 4096); }  Compressing Streams  GZipStream class Giải nén tập tin FileStream sourceFile = File.OpenRead(inFilename); FileStream destFile = File.Create(outFilename); GZipStream compStream = new GZipStream(sourceFile, CompressionMode.Decompress); byte[] Arr = new byte[4096]; int theByte = compStream.Read(Arr, 0, 4096); while (theByte != 0) { destFile.Write(Arr, 0, theByte); theByte = compStream.Read(Arr, 0, 4096); }  Compressing Streams  DeflateStream class     Kết hợp LZ77 Huffman Dùng tương tự GZipStream class Cho kích thước nén nhỏ File nén khơng thể mở chương trình giải nén khác Q&A  Cám ơn bạn theo dõi! ... Stream class FileStream class StreamReader class, StreamWriter class BinaryReader class, BinaryWriter class MemoryStream class BufferedStream class Reading and Writing Files  Stream class ...        DriveInfo class DirectoryInfo class FileInfo class Path class FileSystemWatcher class File class Directory class Navigating the File System  DriveInfo class  Thuộc tính Navigating... Stream class  Phương thức Reading and Writing Files  FileStream class: kế thừa Stream class  Thuộc tính  Phương thức Reading and Writing Files  StreamReader class    Kế thừa TextReader class

Ngày đăng: 08/08/2014, 19:20