1. Các khái niệm cơ bản:
- Stream (luồng): Là cách mà chương trình đọc và ghi dữ liệu. Stream có thể là luồng dữ liệu vào hoặc ra khỏi chương trình.
- InputStream và OutputStream: Là các lớp xử lý luồng byte. InputStream dùng để đọc dữ liệu, OutputStream dùng để viết dữ liệu.
- Reader và Writer: Là các lớp xử lý luồng ký tự. Reader dùng để đọc dữ liệu ký tự, Writer dùng để viết dữ liệu ký tự.
2. Các bước thực hiện I/O trong Java:
1. Tạo file: Xác định đường dẫn và tạo file nếu chưa tồn tại.
2. Mở stream: Tùy vào việc đọc hay ghi mà mở InputStream hoặc OutputStream.
3. Thực hiện đọc/ghi: Dùng các phương thức read() hoặc write() để xử lý dữ liệu.
4. Đóng stream: Luôn đóng stream sau khi sử dụng xong để giải phóng tài nguyên.
3. Stream byte:
4. Stream ký tự:
5. Sự khác biệt giữa stream byte và stream ký tự:
- Stream byte xử lý dữ liệu dưới dạng byte (8-bit), còn stream ký tự xử lý dữ liệu dưới dạng char (16-bit).
- Stream byte thường được dùng cho các file nhị phân như hình ảnh, video. Stream ký tự được dùng cho các file văn bản có chữ cái.
- Stream byte không sử dụng bộ nhớ đệm (cache), còn stream ký tự sử dụng bộ nhớ đệm. Vì vậy, cần đóng stream byte để dữ liệu được lưu, còn stream ký tự cần flush() hoặc đóng stream để dữ liệu được xuất.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IODemo {
@Test
public void docFileByte() throws IOException {
File tapTin = new File("D:/data/test.txt");
if (tapTin.exists()) {
InputStream luuTrongNhap = new FileInputStream(tapTin);
byte[] duLieu = new byte[(int) tapTin.length()];
int soByte = luuTrongNhap.read(duLieu);
luuTrongNhap.close();
System.out.println(new String(duLieu));
}
}
@Test
public void ghiFileByte() throws IOException {
String duongDan = "D:/data";
File folder = new File(duongDan);
if (!folder.exists()) {
folder.mkdirs();
}
File tapTin = new File(duongDan + "/test.txt");
OutputStream luuTrongXuat = new FileOutputStream(tapTin);
String noiDung = "Xin chào Java IO!";
luuTrongXuat.write(noiDung.getBytes());
luuTrongXuat.close();
}
}
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class IODemo {
@Test
public void ghiFileKýTự() throws IOException {
String duongDan = "D:/data";
File folder = new File(duongDan);
if (!folder.exists()) {
folder.mkdirs();
}
File tapTin = new File(duongDan + "/testChar.txt");
Writer vietWriter = new FileWriter(tapTin);
String noiDung = "Tôi đang học Java IO!";
vietWriter.write(noiDung);
vietWriter.close();
}
@Test
public void docFileKýTự() throws IOException {
File tapTin = new File("D:/data/testChar.txt");
if (tapTin.exists()) {
Reader docReader = new FileReader(tapTin);
char[] duLieuKýTự = new char[(int) tapTin.length()];
docReader.read(duLieuKýTự);
System.out.println(new String(duLieuKýTự));
docReader.close();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
public class IODemo {
@Test
public void ghiFileByte() throws IOException {
String duongDan = "D:/data";
File folder = new File(duongDan);
if (!folder.exists()) {
folder.mkdirs();
}
File tapTin = new File(duongDan + "/test1.txt");
OutputStream luuTrongXuat = new FileOutputStream(tapTin);
String noiDung = "Tôi yêu Java!";
luuTrongXuat.write(noiDung.getBytes());
luuTrongXuat.close(); // Dữ liệu được lưu khi đóng stream
}
@Test
public void ghiFileKýTự() throws IOException {
String duongDan = "D:/data";
File folder = new File(duongDan);
if (!folder.exists()) {
folder.mkdirs();
}
File tapTin = new File(duongDan + "/testChar1.txt");
Writer vietWriter = new FileWriter(tapTin);
String noiDung = "Tôi yêu Java!";
vietWriter.write(noiDung);
//vietWriter.close(); // Dữ liệu không được lưu nếu không đóng stream
//vietWriter.flush(); // Dữ liệu được lưu khi flush()
}
}