Tổng quan Java I/O Streams

Nhập dữ liệu trong Java

Anthony Markham

VP Quant Developer

Kiến thức nền tảng về Stream

  • Stream là đường ống dữ liệu một chiều theo thứ tự FIFO (vào trước, ra trước)
  • Byte stream làm việc với dữ liệu nhị phân thô (InputStream/OutputStream)
  • Character stream xử lý văn bản với mã hóa tự động (Reader/Writer)

Minh họa Streams

Nhập dữ liệu trong Java

Byte Streams

  • Dùng cho dữ liệu nhị phân (hình ảnh, âm thanh, ...)
  • Lớp trừu tượng nền tảng: InputStream (đọc), OutputStream (ghi)
  • Triển khai chính: FileInputStream, FileOutputStream
  • Phương thức chính: read() trả về byte, write() ghi byte

Tệp nhị phân: hình ảnh và âm thanh

Nhập dữ liệu trong Java

Byte Streams

try (FileInputStream fis = new FileInputStream("data.bin")) {

byte[] buffer = new byte[1024]; int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) { // Process bytes in buffer
System.out.println("Read " + bytesRead + " bytes"); }
} catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
Nhập dữ liệu trong Java

Character Streams

  • Thiết kế riêng cho dữ liệu văn bản
  • Lớp trừu tượng nền tảng: Reader (đọc văn bản), Writer (ghi văn bản)
  • Triển khai chính: FileReader, FileWriter, BufferedReader, BufferedWriter
  • Phương thức tiện lợi: readLine() đọc cả một dòng văn bản

Tệp CSV

Nhập dữ liệu trong Java

Character Streams

try (BufferedReader reader = new BufferedReader(new FileReader("data.csv"))) {
    String line;

while ((line = reader.readLine()) != null) { // Process each line System.out.println(line); }
} catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
Nhập dữ liệu trong Java

Buffered Streams - hiệu năng

  • Cải thiện hiệu năng bằng cách giảm thao tác đĩa
  • Đọc/ghi stream byte với BufferedInputStreamBufferedOutputStream
  • Đọc/ghi stream ký tự với BufferedReader, BufferedWriter
    • Đọc cả dòng bằng readLine()
Nhập dữ liệu trong Java

Buffered Streams - hiệu năng

try (BufferedReader reader = new BufferedReader(new FileReader("data.csv"))) {
    // Header processing
    String header = reader.readLine();
    System.out.println("Header: " + header);

// Efficient data reading String line; int count = 0; while ((line = reader.readLine()) != null) { count++; } System.out.println("Read " + count + " data rows"); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); }
Nhập dữ liệu trong Java

Xâu chuỗi Stream (stream chaining)

  • Mỗi stream trong chuỗi bổ sung một chức năng cụ thể
  • Mẫu thường dùng: FileInputStream -> InputStreamReader -> BufferedReader
  • Luôn đóng stream ngoài cùng (các stream bên trong sẽ tự đóng)
Nhập dữ liệu trong Java

Xâu chuỗi Stream (stream chaining)

import java.nio.charset.StandardCharsets;
try (

BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream("data.csv"),
StandardCharsets.UTF_8 )
)
) { String line; while ((line = reader.readLine()) != null) { System.out.println(line);} } catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
Nhập dữ liệu trong Java

Xử lý ngoại lệ

  • Tối quan trọng cho thao tác I/O
  • Ngoại lệ thường gặp: IOException, FileNotFoundException
  • Luôn dùng try-with-resources để bắt ngoại lệ
Nhập dữ liệu trong Java

Xử lý ngoại lệ

import java.util.ArrayList;
import java.util.List;
public static List<String> readLines(String filePath) throws IOException {

List<String> lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { lines.add(line); } } // Stream automatically closed here return lines;
}
Nhập dữ liệu trong Java

Cùng luyện tập nào!

Nhập dữ liệu trong Java

Preparing Video For Download...