1. Trang chủ
  2. » Thể loại khác

Com Session 8 File and IO

12 109 0

Đ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

1 Truy xuất thông tin File thư mục 1.1 Truy xuất sửa đổi thông tin file hay thư mục 1.2 Chép, di chuyển, xóa file hay thư mục Đọc ghi liệu với File 2.1 Đọc, ghi file văn 2.2 Đọc, ghi file nhị phân 2.3: Đọc ghi file khả hóa Viet Anh Phan — K12 G) - Tạo tượng đối tượng System.lO.Filelnfo System.lO.Directorylnfo cho cho file hay đối thư mục Sau đó, truyền đường dẫn tới file hay thư mục phương thức khởi dựng Các thơng tin cần thiết truy xuất thông qua thuộc tính đối tượng - namespace System.lO chứa lớp cho phép thao tác với file thư mục Viet Anh Phan — K12 @ Thanh vién Exists Attributes Thuộc lớp Filelnfo va DirectoryInfo Mô tả Trả true hay false, tùy thuộc vào file hay thư mục có tồn đường dẫn định hay không Filelnfo Trả nhiều giá trị thuộc Directorylnfo kê System.IO.FileAttributes, cho thuộc tính file hay thư mục kiểu liệt biết FullName, Name, | Filelnfo và Extension DirectoryInfo Trả chuỗi chứa tên đầy đủ, tên thư mục hay tên file (cùng phần mở rộng), phần mở rộng Length Trả kích thước file (tính theo byte) Filelnfo Viet Anh Phan — K12 G) Thanh vién Thuộc lớp Mô tả DirectoryyName | Directorylnfo Directory DN: Tên thư mục cha, Directory: đối tượng DirectoryInfo mô tả thư mục cha CreateSubdirectory | Directorylnfo Tạo thư mục bên thư mục mô tả GetDirectories Trả mảng DirectoryInfo đối tượng Directorylnfo đối tượng đối tượng mô tả thư mục GetFiles Directorylnfo Directorylnfo, thư mục Trả mảng đối tượng Filelnfo, đố tượng mô tả file thư mục Viet Anh Phan — K12 (4) - Các thuộc tính File, thư mục + archive, system, hidden, read-only, compressed, encrypted -_ Sử dụng thuộc tinh Attributes - Ví dụ Filelnfo file = new Filelnfo(“data.txt'); file Attributes = FileAttributes.eadOnly Viet Anh Phan — K12 G) - CopyTo ¢ Create va CreateText: Create tao file duoc chi dinh trả đối tượng FileStream dùng để ghi file CreateText tạo file trả đối tượng StreamWriter gói lấy stream - Open, OpenRead,OpenText, va OpenWrite - Delete - MoveTo : Chuyén File/thu muc, cé thé dùng để đổi tên file/ thư mục Viet Anh Phan — K12 - Files va Streams: + file nơi lưu trữ thông tin ổ đĩa nhớ + stream: luổng thông tin, stream dùng để gửi, nhận thông tin từ nhớ, mạng, web, - Tại cần file stream? + Dữ liệu nhập từ bàn phím vào chương trình lưu vùng đệm, bị chương trình kết thúc + File lưu trữ liệu chương trình kết thúc tắt máy tính Dữ liệu lưu file dùng cho lần chạy chương trình cho chương trình khác + Stream dùng để lưu trữ nhận liệu từ file Viet Anh Phan — K12 G) §mS < stream - StreamWriter La - StreamReader BinaryReader BinaryWriter Viet Anh Phan — K12 DB System.IO Wtamespace - Để đọc ghi liệu vào file văn theo kiểu mã ASCII, Unicode , thực thao tác sau: + Tạo đối tượng System.IO.FileStream tham chiếu đến file + Ghi file: gói FileStream System.|O.StreamWriter sử dụng phương chong Write + Đọc file: gói FileStream System.l|O.StreamReader sử dụng Read hay ReadLine thức nạp phương thức - Khi su dung StreamReader va StreamWriter định kiểu mã hóa Mặc định UTF-8 Các kiểu mã hóa NET cung cấp namespace System Text Viet Anh Phan — K12 @) class Program { static void Main(string[] args) { FileStream stream = new FileStream(@"C:\data.txt",FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(stream, Encoding.Unicode); writer.WriteLine("Lớp Tin 13"); writer.Write("Số sinh viên = "); writer.Write(120); writer.Close(); stream.Close(); Console.Read(); Viet Anh Phan — K12 class Program { static void Main(string[] args) { FileStream stream = new FileStream(@"C:\data.txt",FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream, Encoding.Unicode); string dt = reader.ReadLine(); dt = reader.ReadLine(); reader.Close(); reader.Close(); Console.Read(); Viet Anh Phan — K12 (1) - Tạo đối tượng đến System.IO.FileSteam tham chiếu file - Ghi file: FileStream System.|O.BinaryWriter sử dụng phương thức nạp chồng Write - Đọc file: FileStream sử dụng liệu System.IO.BinaryReader phương thức Read phù hợp với kiểu Viet Anh Phan — K12 (2 ) ... đối tượng đến System .IO. FileSteam tham chiếu file - Ghi file: FileStream System.|O.BinaryWriter sử dụng phương thức nạp chồng Write - Đọc file: FileStream sử dụng liệu System .IO. BinaryReader phương... K12 DB System .IO Wtamespace - Để đọc ghi liệu vào file văn theo kiểu mã ASCII, Unicode , thực thao tác sau: + Tạo đối tượng System .IO. FileStream tham chiếu đến file + Ghi file: gói FileStream System.|O.StreamWriter... Directorylnfo kê System .IO. FileAttributes, cho thuộc tính file hay thư mục kiểu liệt biết FullName, Name, | Filelnfo và Extension DirectoryInfo Trả chuỗi chứa tên đầy đủ, tên thư mục hay tên file (cùng phần

Ngày đăng: 18/01/2018, 13:04

Xem thêm:

w