Xử lý tập tin
Trang 1Xử lý tập tin
Trang 2Nội dung
Đọc và ghi file
Trang 3Navigating the File System
Trang 4Navigating the File System
Trang 5Navigating the File System
DriveInfo class
Cung cấp các thông tin khác nhau về các drive
Thuộc tính
Trang 6Navigating the File System
DriveInfo class
DriveType enum
Phương thức
Trang 7Navigating the File System
Là lớp cơ sở của lớp FileInfo và DirectoryInfo
Thuộc tính
Trang 8Navigating the File System
Phương thức
Trang 9Navigating the File System
DirectoryInfo class
Cung cấp thông tin và các lệnh làm việc vớithư mục
Thuộc tính
Trang 10Navigating the File System
DirectoryInfo class
Phương thức
Trang 11Navigating the File System
FileInfo class
Cung cấp thông tin và các lệnh làm việc với file
Thuộc tính
Trang 12Navigating the File System
FileInfo class
Phương thức
Trang 13Navigating the File System
Thao tác với đường dẫn tới file, thư mục
Phương thức
Trang 14Navigating the File System
Phương thức
Trang 15Navigating the File System
Theo dõi các thay đổi liên quan tới thư mục và các file chứa trong đó
Thuộc tính
Trang 16Navigating the File System
Phương thức
Sự kiện
Trang 17Navigating the File System
Làm sao biết được trên hệ thống có những ổ đĩa nào?
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
Trang 18Navigating the File System
Làm sao lấy được danh sách tập tin và thư mục con của một thư mục nào đó?
DirectoryInfo ourDir = new DirectoryInfo(@"c:\windows");
Trang 19Navigating the File System
Làm sao truy xuất được các thuộc tính của một tập tin, thư mục?
FileInfo ourFile = new FileInfo(@"c:\boot.ini ");
if (ourFile.Exists)
{
Console.WriteLine("Filename : {0}",
ourFile.Name); Console.WriteLine("Path : {0}",
ourFile.FullName); }
Trang 20Navigating the File System
Làm sao giám sát được sự thay đổi của một tập tin, thư mục?
FileSystemWatcher watcher = new FileSystemWatcher();
Trang 21Navigating the File System
Cung cấp tất cả các phương thức cần thiết cho
việc tạo file, xóa file, di chuyển file, sao chép file…
Cung cấp tất cả các phương thức cần thiết cho
việc tạo thư mục, xóa thư mục, di chuyển thư mục, lấy danh sách tập tin, thư mục con…
Trang 22Reading and Writing Files
Các lớp hỗ trợ
Stream class
FileStream class
StreamReader class, StreamWriter class
BinaryReader class, BinaryWriter class
MemoryStream class
BufferedStream class
Trang 23Reading and Writing Files
Cung cấp các thao tác cơ bản với một chuỗi các byte: đọc, ghi và dịch chuyển (seeking)
Thuộc tính
Trang 24Reading and Writing Files
Phương thức
Trang 25Reading and Writing Files
Thuộc tính
Phương thức
Trang 26Reading and Writing Files
Kế thừa TextReader class
Dùng để đọc các file văn bản theo bảng mã xác định, mặc định là UTF-8
Thuộc tính
Trang 27Reading and Writing Files
Phương thức
Trang 28Reading and Writing Files
Trang 29Reading and Writing Files
Kế thừa TextWriter class
Dùng để ghi các file văn bản theo bảng mã xác định, mặc định là UTF-8
Thuộc tính
Trang 30Reading and Writing Files
Phương thức
Trang 31Reading and Writing Files
Ví dụ
Ghi file văn bản
FileStream theFile = File.Open (@"c:\somefile.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter (theFile); writer.WriteLine("Hello");
writer Close ();
theFile Close ();
Trang 32Reading and Writing Files
Trang 33Reading and Writing Files
long number = reader ReadInt64 ();
byte[] bytes = reader ReadBytes (4);
string s = reader ReadString ();
Trang 34Reading and Writing Files
Dùng để ghi file nhị phân
Cung cấp các phương thức Write với nhiều dạng tham số khác nhau để ghi nội dung file
Trang 35Reading 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);
Trang 36Reading and Writing Files
MemoryStream class: kế thừa Stream class, ghi stream vào bộ nhớ
Thuộc tính
Phương thức
Trang 37Reading and Writing Files
Ví dụ
MemoryStream memStrm = new MemoryStream(); StreamWriter writer = new StreamWriter(memStrm); writer.WriteLine("Hello");
Trang 38Reading and Writing Files
BufferedStream class
Thường dùng cho NetworkStream
Tăng hiệu quả đọc/ghi dữ liệu
new StreamWriter(buffered);
writer WriteLine ("Some data");
Trang 40Compressing 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);
}
Trang 41Compressing 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);
}
Trang 42Compressing Streams
Kết hợp giữa LZ77 và Huffman
Dùng tương tự như GZipStream class
Cho kích thước nén nhỏ hơn
File nén không thể mở được bằng các chương trình giải nén khác