CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 366

366
Chương 9: File, thư mục, và I/O

w.Write("Test string 2");

w.Write('!');

// Bảo đảm tất cả dữ liệu được ghi từ buffer.

w.Flush();

// Đóng file.

w.Close();

fs.Close();

Console.WriteLine("Press Enter to read the information.");

Console.ReadLine();

// Mở file trong chế độ chỉ-đọc.

fs = new FileStream("test.txt", FileMode.Open);

// Hiển thị dữ liệu thô trong file.

StreamReader sr = new StreamReader(fs);

Console.WriteLine(sr.ReadToEnd());

Console.WriteLine();

// Đọc dữ liệu và chuyển nó về kiểu thích hợp.

fs.Position = 0;

BinaryReader br = new BinaryReader(fs);

Console.WriteLine(br.ReadDecimal());

Console.WriteLine(br.ReadString());

Console.WriteLine(br.ReadString());

Console.WriteLine(br.ReadChar());

fs.Close();

Console.ReadLine();

}

}