0% found this document useful (0 votes)
5 views13 pages

File Handling in Java: Read & Write

Uploaded by

thang
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

File Handling in Java: Read & Write

Uploaded by

thang
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

File Binary

File nhị phân


• File
• Stream
• biến
Ghi file: Các bước thực hiện
• Mở file để ghi
• Ghi file
• Đóng file
Đọc file: Các bước thực hiện
• Mở file để đọc
• Đọc file
• Đóng file
Ghi file
FileOutputStream

Thư viện thường dùng


Đọc file
FileInputStream
Đọc file
Ví dụ 1
void ghiFile() throws IOException { //ghi file
DataOutputStream outStream = new
DataOutputStream(new FileOutputStream
("[Link]”));//,[Link]));
for (int k = 0; k < 5; k++) { // Output 5 data records
[Link]("Name" + k); // Name
[Link]((int)(20 + [Link]() * 25));
[Link]([Link]() * 500);
}
[Link]();
}
void docFile() {
try{
DataInputStream inStream = new
DataInputStream(new
FileInputStream("[Link]"));
try{
while (true) {
String name = [Link]();
int age = [Link]();
double pay = [Link]();
[Link](name+ " " + age + " " + pay + "\n");
} // while
}catch(EOFException e) {}
finally{ [Link]();}} catch (IOException e) {} }
public class SinhVien {
private String ma;
private String ho;
private String ten;
//…
void ghiFile() throws IOException { //ghi file
DataOutputStream outStream = new DataOutputStream(new
FileOutputStream ("[Link]",[Link]));
[Link](ma);
[Link](ho);
[Link](ten);
[Link]();
}
public class DSSV {
SinhVien [] dssv;
int n;
DSSV(){}
void nhap(){}
void xuat(){}
void ghiFile() throws IOException { //ghi file
DataOutputStream outStream = new DataOutputStream(new
FileOutputStream ("[Link]"));
for (int i = 0; i < n; i++) { //
//[Link](dssv[i].getMa());
//[Link](dssv[i].getHo());
//[Link](dssv[i].getTen());
dssv[i].ghiFile(); }
[Link]();
}
void docFile() {
int i=0;
try {
DataInputStream inStream= new DataInputStream(new
FileInputStream("[Link]"));
try{
while (true){//xin cap phat them
dssv[i]=new SinhVien();
dssv[i].setMa([Link]());
dssv[i].setHo([Link]());
dssv[i].setTen([Link]());
dssv[i].xuat();
i++; } // while }
catch(EOFException e) {} // Close the stream
finally{ n=i; [Link](); } }//try
catch (IOException e) {}
}/try }//docfile

You might also like