Ghi/ Đọc file text a Ghi dữ liệu vào fileCú pháp để ghi dữ liệu vào file:string filename = “Notes.txt”;StreamWriter tex = new StreamWriterfilenametex.Write“Chao mung ban den voi C#”; Sau
Trang 1BỘ GIÁO DỤC VÀ ĐÀO TẠO
TRƯỜNG ĐẠI HỌC KINH TẾ THÀNH PHỐ HỒ CHÍ MINH
TIỂU LUẬN CUỐI KỲMÔN CƠ SỞ LẬP TRÌNHLỚP HP: 22C1INF50900507
PHÒNG: N1-303
DANH SÁCH THÀNH VIÊN NHÓM
1 Trịnh Khang Bảo – 31211023332 – SĐT: 0822558822 2 Đặng Thanh Huy – 31211026749
3 Lê Phi Long – 31211027154
4 Nguyễn Trung Nguyên – 31211026558
Trang 2using System.IO;
1 Ghi/ Đọc file text
a) Ghi dữ liệu vào file
Cú pháp để ghi dữ liệu vào file:
string filename = “Notes.txt”;
StreamWriter tex = new StreamWriter(filename)tex.Write(“Chao mung ban den voi C#”);
Sau khi ghi xong cần phải đóng file lại.
tex.Close();
b) Đọc dữ liệu từ file
string filename= “Notes.txt”;
StreamReader tex1 = new StreamReader(filename); tex1.ReadLine();
internal class Program {
static void Main(string[] args) {
string myfile = "Notes.txt";
Trang 3//ghi file
int a = 2022; double b =20.22 ; char c = ;'A'
StreamWriter SW = new StreamWriter(myfile); SW.WriteLine("Hello World");
SW.WriteLine("Doc ghi file trong C#"); SW.WriteLine(a);
SW.WriteLine(b); SW.WriteLine(c);
while ((input = SR.ReadLine()) != null) {
Console.WriteLine(input); }
Console.WriteLine("Ket thuc chuong trinh"); SR.Close();
Console.ReadLine(); }
}
Trang 4Ket thuc chuong trinh
Too long to read onyour phone? Save
to read later onyour computer
Save to a Studylist
Trang 5Ngoài ra, còn có một cách khác để ghi dữ liệu vào file Trước hết ta phải khai báo biến file:
StreamWrite SW;
Tạo file text nhờ hàm CreateText();static string myfile = “Notes.txt”;SW = File.CreateText(myfile);
Ghi dữ liệu vào file:
SW.WriteLine(“Hello World”);
Sau khi ghi xong cần phải đóng file lại.
Đọc dữ liệu từ file:Khai báo đối tượng file:
Trang 6namespace COSOLAPTRINH{
internal class Program {
static void Main(string[] args) {
string myfile = "Notes.txt";
//ghi file
StreamWriter SW;
SW = File.CreateText(myfile); SW.WriteLine("Welcome to UEH"); SW.WriteLine("Nhom 7 Khoa BIT"); SW.Close();
//doc file
Console.WriteLine("Du lieu doc tu file la : "); StreamReader SR = new StreamReader(myfile); string input ;
SR = File.OpenText(myfile); input = SR.ReadToEnd(); Console.WriteLine(input);
Console.WriteLine("Ket thuc chuong trinh"); SR.Close();
Console.ReadLine(); }
}}
Trang 7Kết quả:
Du lieu doc tu file la:Welcome to UEHNhom 7 Khoa BITKet thuc chuong trinh
Ta cũng có thể đọc file theo đoạn mã dưới đây:
StreamReader SR;string input;
SR = File.OpenText(myfile);while (true)
if (input == null) break;Console.WriteLine(input);}
SR.Close();
Trang 8internal class Program {
static void Main(string[] args) {
//ghi file
FileInfo myfile = new FileInfo("Notes.txt"); StreamWriter SW = myfile.CreateText(); SW.Write(SW.NewLine);
SW.WriteLine("Welcome to UEH "); SW.WriteLine("Day la nhom 7 EE003");
SW.Close(); Console.Read();
//doc file
Console.WriteLine("Du lieu doc tu file la : "); StreamReader SR = File.OpenText("Notes.txt"); string input = null;
while ((input = SR.ReadLine()) != null) {
Console.WriteLine(input); }
SR.Close(); Console.ReadKey(); }
}}
Trang 9Kết quả:
Du lieu doc tu file la:Welcome to UEHDay la nhom 7 EE003
Trang 102 Đọc/ Ghi file Nhị phân
Tập tin nhị phân là một tập tin không phải là tập tin văn bản, tập tin được lưu trữ ở dữ liệu thô gồm các số 0 và 1.
Việc ghi/ đọc dữ liệu của file nhị phân có nhiều ưu điểm hơn file text vìcó tính bải mật cao hơn Sau đây là cách ghi/ đọc dữ liệu cho một byte vàcách ghi/ đọc dữ liệu cho một đối tượng chứa nhiều trường dữ liệu.
a)Ghi/ Đọc dữ liệu file kiểu byte:
- Ghi dữ liệu vào file kiểu byte:
+ Giả sử ta khai báo một biến mảng kiểu byte:
byte [] a = { 255, 47, 29, 199, 99 };
+ Khai báo đối tượng để ghi dữ liệu vào file “testfile.dat” ở ổ đĩa C:
FileStream(“test.dat”, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
+ Thực hiện quá trình ghi các phần tử của mảng số a có kiểu byte:
foreach(byte b in a){
np.WriteByte(b);}
i= st.ReadByte(); if (i == -1) break; Console.Write(“{0,4}”, i);}
+ Đóng file sau khi đọc xong:
st.Close();
Trang 11Ví dụ 4: Ghi và đọc các số có kiểu byte (giá trị 0 – 255)
internal class Filebin {
static void Main(string[] args) {
byte[] a = { 255, 47, 29, 199, 99 }; FileStream ab = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); ab.Write(a);
ab.Close();
Console.ReadLine(); //Doc du lieu tu file
FileInfo b = new FileInfo("test.dat"); Stream cd = b.OpenRead(); int i;
while true ( ) {
i = cd.ReadByte(); (i == -1) if break;
Console.Write("{0, 4}", i); }
cd.Close();
Console.ReadLine(); }
}}
Trang 12Kết quả:
255 47 29 199 99
b)Ghi/ đọc dữ liệu của nhiều kiểu dữ liệu khác nhau bằng file nhị phân.
- Ghi dữ liệu từ file nhị phân
String myfile = “c:\\testfile.dat”;fileStream myfile;
string name; //dữ liệu thành viêndouble tienluong; //dữ liệu thành viên
Trang 13// Khai báo đối tượng wf cho việc ghi vào file nhị phân
BinaryWrite wf = new BinaryWrite(myfile);
//Các hàm Write () để ghi từng thành viên
Wf.Write(name);wf.Write(tienluong);
- Đọc dữ liệu từ file nhị phân:+ Khai báo file cần đọc
FileStream myfile;
+ Khai báo đối tượng rf cho việc đọc:
BinaryReader rf = new BinaryReader(myfile);
+ Các hàm để đọc file nhị phân:
Name = rf.ReadString();Tienluong = rf.ReadDouble();
+ Phụ thuộc vào kiểu khai báo từng dữ liệu mà ta có các hàm đọc dữ liệu khác nhau.
Byte b; b= rf.ReadByte();Char ch; ch = rf.ReadChar();Decimal d1; d1= rf.ReadDecimal();Double d2; d2 = rf.ReadDouble();Float f; f = rf.ReadFSingle();Int a; a = rf.ReadInt32();Long b; b=rf.ReadInt64();Sbyte sb; sb= rf.ReadSByte();Uint ui; ui= rf.ReadUInt32();Ulong ul ; ul= rf.ReadUInt64() ;
internal class Filebin {
Trang 14static void Main(string[] args) {
Console.WriteLine("Vi du minh hoa doc va ghi binary file trong C#");
BinaryWriter bw; BinaryReader br; int i = 19; double d = 1.412; bool b = true;
string s = "Ghi va doc binary file"; //tao file co ten la mydata
Console.WriteLine(e.Message + "\n Khong the tao file."); return;
catch (IOException e) {
Console.WriteLine(e.Message + "\n Khong the ghi du lieu vao file.");
return;
Trang 15}
bw.Close(); //doc file
Console.WriteLine(e.Message + "\n Khong the mo file."); return;
} try
Console.WriteLine(e.Message + "\n Khong the doc file."); return;
}
br.Close();
Console.ReadKey(); }
}}
Trang 17Kết quả :
Ví dụ minh hoa doc va ghi binary file trong C#Du lieu int : 19
Du lieu double : 1.412Du lieu boolean : True
Du lieu string : Ghi va doc binary file