LẬP TRÌNH JAVA
Chương 5
NHẬP XUẤT
GV: Đoàn Thanh Sơn
Nội dung
Tổ chức File
Truy cập File
I/O stream
I/O Character
2/25
Tổ chức File
File
java.io.File
• Dùng để biểu diễn tên file hoặc thư mục
Hàm khởi tạo
• File(String pathname);
• File(String parent, String child);
• File(File parent, String child);
Ví dụ:
File file = new File("c:/aFolder/aFile.txt");
4/25
File
Ví dụ:tạo Thư mục
public static void main(String[] as)throws
IOException{
File dir=new File("G:\\demo");
dir.mkdir();
}
5/25
File
boolean exists(): kiểm tra có tồn tại tên file hay thư
mục
String getAbsolutePath(): lấy đường dẫn tuyệt đối
String getName(): trả về tên file hay thư mục
String getParent(): trả về tên thư mục cha
boolean isDirectory(): kiểm tra có phải là thư mục
hay không
boolean isFile(): kiểm tra có phải file
String[] list(): liệt kê file & thư mục con
6/25
File
Ví dụ:tạo File
public static void main(String[] as)throws
IOException{
File dir=new File("G:\\Test");dir.mkdir();
File f=new File("G:\\Test\\thu.txt");
/*File f=new File("G:\\Test","thu1.txt");
File f=new File(dir,"thu2.txt");*/
f.createNewFile();
}
7/25
File
boolean canRead(): kiểm tra file có thể đọc
boolean canWrite():kiểm tra file có thể ghi
boolean canExecute():kiểm tra file có thể thực thi
boolean delete(): xoá file hoặc thư mục
long length(): trả lại chiều dài của đường dẫn
boolean mkdir(): tạo thư mục
boolean renameTo(File newname): đổi tên file hoặc thư mục
8/25
File
Tạo trước thư mục "demo" trong ổ E, tạo 1 vài file và t
hư mục trong đó
public static void main(String[] args) throws IOExcepti
on {
File dir = new File("E:\\demo");
File[] listFile = dir.listFiles();
System.out.println("Danh sách file trong thư
mục E:\\demo là: ");
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isFile()) {
System.out.println(listFile[i].getNam
e());
}
}
9/25
File
System.out.println("Danh sách thư mục trong
thư mục E:\\demo là: ");
for (int i = 0; i < listFile.length;
i++) {
if (listFile[i].isDirectory()) {
System.out.println(listFile[
i].getName());
}
}
}
10/25
Truy cập File
Giới thiệu
Nhập xuất trong Java được phân loại
• Theo thứ tự truy xuất
Truy xuất ngẫu nhiên – Random Access Files: cho phép
đọc ghi tại bất kỳ vị trí nào
Truy xuất tuần tự: đọc ghi theo thứ tự
• Theo đặc điểm dữ liệu
Nhập xuất nhị phân (Nhập xuất byte)
Nhập xuất ký tự
12/25
Giới thiệu
Các lớp nhập xuất trong Java
• Thuộc gói java.io.*
• Được tổ chức theo cấu trúc kế thừa
Cấp thấp: đọc ghi trực tiếp trên thiết bị
Cấp cao: đọc ghi thông qua bộ đệm
13/25
Random Access Files
Random Access Files
Cho phép ta truy nhập trực tiếp vào các tệp, có thể đọc, ghi các byte
ở bất kỳ vị trí nào trong tệp.
Hiện thực các interface DataInput, DataOutput.
RandomAccessFile hỗ trợ vấn đề định vị con trỏ file bên trong một file
dùng phương thức seek(long newPos).
15/25
Random Access Files
Hàm khởi tạo
• RandomAccessFile(String name, String
mode) throws IOException
• RandomAccessFile(File file, String
mode) throws IOException
Tham số mode:
-“r”: Dùng để đọc.
-“rw”: Dùng để ghi.
16/25
Random Access Files
Các phương thức
• long getFilePointer() throws IOException : Trả
về vị trí của con trỏ tệp.
• long length() throws IOException: cho biết số
byte hay độ dài của tệp.
• void seek(long offset) throws IOException:
Chuyển con trỏ tệp đi offset vị trí kể từ đầu tệp.
• void close() throws IOException: Khi không cần
truy nhập tệp nữa thì đóng lại.
17/25
Random Access Files
public static void main(String args[]) throws IOException {
double data[] = {11.2, 13.6, 255.6, 117.92, 2007.96,
8.9, 9.9, 10.0, 100.6};
double d;
RandomAccessFile raf;
try {
raf = new RandomAccessFile("E:\\random.dat", "rw");
} catch (FileNotFoundException exc) {
System.out.println("Cannot open file.");
return;
}
18/25
Random Access Files
// Write values to the file.
for (int i = 0; i < data.length; i++) {
try {
raf.writeDouble(data[i]);
} catch (IOException exc) {
System.out.println("Error writing to file.");
return;
}
}
19/25
Random Access Files
System.out.println("Read all: ");
for (int i = 0; i < data.length; i++) {
raf.seek(8 * i); // seek to ith double
d = raf.readDouble();
System.out.print(d + " ");
}
System.out.println("");
20/25
Luồng - Stream
I/O Stream
Stream là 1 dãy dữ liệu
Input stream cho việc đọc dữ liệu
Output stream cho việc ghi dữ liệu
22/25
I/O Stream
Phân loại
• Luồng byte (Byte streams)
Đọc ghi dữ liệu theo đơn vị byte
thừa kế từ: InputStream & OutputStream
Để đọc và ghi những giá trị kiểu dữ liệu trong java, chúng ta
sử dụng DataInputStream và DataOutputStream
• Luồng ký tự (Character streams)
Đọc ghi dữ liệu theo đơn vị ký tự
thừa kế từ: Reader & Writer
23/25
I/O Stream
I/O Stream
25/25