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ự
Trang 1LẬP TRÌNH JAVA
Chương 5
NHẬP XUẤT
GV: Võ Hoàng Phương Dung
Trang 2 Giới thiệu
File
Nội dung
Trang 3 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ự
Giới thiệu
Trang 4 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
Giới thiệu
Trang 5 java.io.File
• Dùng để biểu diễn tên file hoặc thư mục
• File(String pathname);
• File(String parent, String child);
• File(File parent, String child);
File
Trang 6 boolean exists(): kiểm tra có tồn tại tên file
hay thư mục
đối
File
Trang 7 boolean canRead(): kiểm tra file có thể đọc
thi
hoặc thư mục
File
Trang 8 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
• 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.
Random Access Files
Trang 9 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.
Random Access Files
Trang 10 Stream là 1 dãy dữ liệu
I/O Stream
Trang 11I/O Stream
Trang 12 Phân loại
• Luồng byte (Byte streams)
Đọc ghi dữ liệu theo đơn vị byte
Tất cả các lớp thừa kế từ: InputStream & OutputStream
• Luồng ký tự (Character streams)
Đọc ghi dữ liệu theo đơn vị ký tự
Tất cả các lớp thừa kế từ: Reader & Writer
I/O Stream
Trang 13 Ví dụ về luồng Byte
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(“input.txt");
out = new FileOutputStream(“byteOutput.txt");
int c;
while ((c = in.read()) != -1) {out.write(c); } }
finally {
if (in != null) { in.close(); }
I/O Stream
Trang 14I/O Stream
Trang 15 Ví dụ về luồng ký tự
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader(“input.txt");
out = new FileWriter(“characterOutput.txt");
int c;
while ((c = in.read()) != -1){ out.write(c); } }
finally {
if (in != null) { in.close(); }
I/O Stream
Trang 16 Các lớp cơ bản của luồng Byte
I/O Stream
FileInputStream Luồng nhập cho phép đọc dữ liệu từ File
FileOutputStream Luồng xuất cho phép ghi dữ liệu xuống File
ByteArrayInputStream Luồng nhập dữ liệu từ 1 mảng byte
ByteArrayOutputStream Luồng xuất dữ liệu ra 1 mảng byte
ObjectInputStream Luồng nhập các Object
ObjectOutputStream Luồng xuất các Object
BufferedInputStream Luồng nhập có đệm
BufferedOutputStream Luồng xuất có đệm
DataInputStream Luồng nhập dữ liệu là các kiểu dữ liệu chuẩn
DataOutputStream Luồng xuất dữ liệu là các kiểu dữ liệu chuẩn
Trang 17 Các lớp cơ bản của luồng ký tự
I/O Stream
Tên lớp Ý nghĩa
BufferedReader Luồng nhập có đệm
BufferedWriter Luồng xuất có đệm
CharArrayReader Luồng nhập dữ liệu từ 1 mảng ký tự
InputStreamReader Luồng nhập chuyển luồng byte thành luồng ký tự
OutputStreamWriter Luồng xuất chuyển luồng ký tự thành luồng byte
StringReader Luồng đọc dữ liệu từ chuỗi
StringWriter Luồng xuất dữ liệu ra chuỗi
FileReader Luồng đọc dữ liệu từ File
FileWriter Luồng xuất dữ liệu ra File
LineNumberReader Luồng nhập đếm dòng
Trang 18 Nhập dữ liệu từ màn hình Console
import java.io.*;
class ReadBytes {
public static void main(String args[])
throws IOException {
byte data[] = new byte[100];
System.out.print("Enter some characters.");
System.in.read(data);
System.out.print("You entered: ");
for(int i=0; i < data.length; i++)
System.out.print((char) data[i]);
}
}
I/O Stream
Trang 19import java.io.*;
class ReadBuffer {
public static void main(String args[]) throws IOException {
BufferedReader in=new BufferedReader (new
InputStreamReader(System.in));
System.out.println("NHAP A:");
int a=Integer.parseInt (in.readLine());
System.out.println("NHAP B:");
int b=Integer.parseInt (in.readLine());
int ketqua;
ketqua=a+b;
System.out.println("ket qua bai toan a+b
la:"+ketqua);
}
I/O Stream