Random Access File
1. แนวคิดพื้นฐานของ Random Access File
Random Access File คือคลาสที่อนุญาตให้เราเข้าถึงข้อมูลในไฟล์แบบสุ่มได้ เปรียบเสมือนหนังสือที่เราสามารถเปิดไปหน้าใดก็ได้โดยตรง โดยไม่จำเป็นต้องอ่านทีละหน้าตั้งแต่ต้นเล่ม
1.1 การทำงานเบื้องต้น
// การสร้าง Random Access File ขั้นพื้นฐาน
RandomAccessFile file = new RandomAccessFile("data.dat", "rw");โหมดการทำงานมีสองแบบ:
- “r” - สำหรับอ่านอย่างเดียว
- “rw” - สำหรับทั้งอ่านและเขียน
2. การจัดการตำแหน่งในไฟล์ (File Pointer)
2.1 หลักการทำงานของตัวชี้ตำแหน่ง
// การอ่านตำแหน่งปัจจุบัน
long currentPosition = file.getFilePointer();
// การเลื่อนไปยังตำแหน่งที่ต้องการ
file.seek(100); // เลื่อนไปที่ไบต์ที่ 1002.2 การคำนวณตำแหน่งสำหรับข้อมูลแบบระเบียน
public class RecordPositionManager {
private static final int RECORD_SIZE = 100; // ขนาดระเบียนคงที่
public static long calculatePosition(int recordNumber) {
return (recordNumber - 1) * RECORD_SIZE;
}
}3. การอ่านและเขียนข้อมูลพื้นฐาน
3.1 การเขียนข้อมูลประเภทต่างๆ
public class BasicDataWriter {
private RandomAccessFile file;
public void writeBasicTypes() throws IOException {
file.writeInt(123); // เขียนเลขจำนวนเต็ม (4 ไบต์)
file.writeDouble(45.67); // เขียนทศนิยม (8 ไบต์)
file.writeChar('A'); // เขียนตัวอักษร (2 ไบต์)
file.writeUTF("สวัสดี"); // เขียนข้อความ UTF-8
}
}3.2 การอ่านข้อมูลประเภทต่างๆ
public class BasicDataReader {
private RandomAccessFile file;
public void readBasicTypes() throws IOException {
int number = file.readInt();
double decimal = file.readDouble();
char character = file.readChar();
String text = file.readUTF();
}
}4. การจัดการข้อมูลแบบระเบียน (Records)
4.1 การออกแบบโครงสร้างระเบียน
public class StudentRecord {
private static final int NAME_LENGTH = 30;
private static final int RECORD_SIZE = 42; // 30 + 4 + 8 ไบต์
private String name; // 30 ไบต์
private int age; // 4 ไบต์
private double gpa; // 8 ไบต์
}4.2 การเขียนระเบียน
public class RecordWriter {
private RandomAccessFile file;
public void writeRecord(StudentRecord student, int recordNumber)
throws IOException {
// คำนวณตำแหน่งที่จะเขียน
long position = (recordNumber - 1) * StudentRecord.RECORD_SIZE;
file.seek(position);
// เขียนข้อมูลแต่ละส่วน
writeFixedString(student.getName(), StudentRecord.NAME_LENGTH);
file.writeInt(student.getAge());
file.writeDouble(student.getGpa());
}
private void writeFixedString(String text, int length) throws IOException {
// เพิ่มช่องว่างให้ครบความยาวที่กำหนด
StringBuilder sb = new StringBuilder(text);
sb.setLength(length);
file.writeBytes(sb.toString());
}
}5. การจัดการข้อผิดพลาดและการใช้งานที่ปลอดภัย
5.1 การตรวจสอบขนาดไฟล์และตำแหน่ง
public class SafeFileAccess {
private RandomAccessFile file;
public boolean isValidPosition(long position) throws IOException {
return position >= 0 && position < file.length();
}
public void seekSafely(long position) throws IOException {
if (!isValidPosition(position)) {
throw new IllegalArgumentException(
"ตำแหน่งไม่ถูกต้อง: " + position);
}
file.seek(position);
}
}5.2 การจัดการทรัพยากรอย่างเหมาะสม
public class ResourceManagement {
public void safeFileOperation() {
try (RandomAccessFile file = new RandomAccessFile("data.dat", "rw")) {
// ดำเนินการกับไฟล์
} catch (IOException e) {
// จัดการข้อผิดพลาด
}
// ไฟล์จะถูกปิดโดยอัตโนมัติ
}
}6. ตัวอย่างการใช้งานจริง: ระบบจัดการข้อมูลนักเรียน
public class StudentManagementSystem {
private RandomAccessFile file;
private static final String FILE_NAME = "students.dat";
public StudentManagementSystem() throws IOException {
file = new RandomAccessFile(FILE_NAME, "rw");
}
public void addStudent(Student student, int studentId) throws IOException {
// เขียนข้อมูลนักเรียนที่ตำแหน่งที่กำหนด
long position = (studentId - 1) * Student.RECORD_SIZE;
file.seek(position);
// เขียนข้อมูล
writeStudentData(student);
}
public Student getStudent(int studentId) throws IOException {
// อ่านข้อมูลนักเรียนจากตำแหน่งที่กำหนด
long position = (studentId - 1) * Student.RECORD_SIZE;
file.seek(position);
// อ่านและสร้างออบเจกต์ Student
return readStudentData();
}
private void writeStudentData(Student student) throws IOException {
// เขียนข้อมูลแต่ละส่วนของนักเรียน
}
private Student readStudentData() throws IOException {
// อ่านและสร้างออบเจกต์ Student
return new Student();
}
public void close() throws IOException {
if (file != null) {
file.close();
}
}
}ปรับปรุงล่าสุด