[ジャワ] Javaオブジェクト内に記録読む – Javaで書き込みオブジェクトを読み取る

読み我々が通信する必要がある場所にあるオブジェクトのJava Objectクラスに記録するには java.io.Serializableの

読み取りレコードの簡単な例 1 オブジェクトMyStudent次のように:

package VietSource.net.File; 	// cac ban nho thay package cho phu hop

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ReadWriteObject {
	public static void main(String[] args){
		MyStudent myStudent = new MyStudent(); // tao doi tuong myStudent

		//Ghi Object vao file

		try {	// dat try cacth de tranh ngoai le khi tao va viet File
			FileOutputStream f = new FileOutputStream("student.dat"); // tao file f tro den student.dat
			ObjectOutputStream oStream = new ObjectOutputStream(f);	// dung de ghi theo Object vao file f
			oStream.writeObject(myStudent);	// ghi MyStudent theo kieu Object vao file
			oStream.close();
		} catch (IOException e) {
			System.out.println("Error Write file");
		}

		// doc Object tu file
		MyStudent ms = null;

		try {	// dat try cacth de tranh ngoai le khi tao va doc File
			FileInputStream f = new FileInputStream("student.dat");	// tao file f tro den student.dat
			ObjectInputStream inStream = new ObjectInputStream(f);	// dung de doc theo Object vao file f
			// dung inStream doc theo Object, ep kieu tra ve la MyStudent
			ms = (MyStudent) inStream.readObject();
			inStream.close();
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");
		} catch (IOException e) {
			System.out.println("Error Read file");
		}

		// Xuat KQ
		System.out.println("My name is " + ms.name + ". I am " + ms.age + " years old");
	}
}

class MyStudent implements Serializable{
	String name = "Nguyen Van Quan";
	int age = 21;
}

結果: 私の名前はグエン・ヴァン・クアンです. わたし 21 歳
注意: あなたがファイルを開くと student.dat あなたはこの種のデータが検索されます: ��

読み出しおよび書き込み方法のより具体的な例, そこに、この例では 1 Studentクラスは、属性の名前と年齢とgetメソッドが含まれています, その値を設定します. Một class ProcessStudent gồm các phương thức cho phép nhập các student từ bàn phím, ghi các student vào file, đọc từ file theo kiểu Object và cuối cùng là hiển thị lại thông tin đọc được. Trong code đã giải thích khá rõ ràng:

package VietSource.net.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;

public class IOFileObject_Student {
	public static void main(String args[]) throws Exception {
		ProcessStudent ps = new ProcessStudent();	// tao doi tuong cua lop ProcessStudent
		Student[] student1 = null, student2 = null;	// tao 2 doi tuong student
		student1 = ps.creat();	// nhap student
		ps.write(student1);		// viet vao file
		student2 = ps.read();	// doc tu file
		ps.show(student2);		//hien thi kq
	}
}

class Student implements Serializable {		// Serializable dung de ghi va doc theo Object
	private static final long serialVersionUID = 1L;	// ID of Serializable
	private String name;
	private int age;

	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	// cac ham get, set gia tri cac bien
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

// class xu ly thong tin

class ProcessStudent {

	public Student[] creat() {		// Input the data from Keyboard
		int n;
		Scanner scan = new Scanner(System.in);

		System.out.print("Enter number of student: ");
		n = Integer.parseInt(scan.nextLine());	//nhap so sinh vien

		Student[] student = new Student[n];

		for (int i = 0; i < n; i++) {
			System.out.print("Enter name: ");	// nhap ten sv thu i
			String name = scan.nextLine();
			System.out.print("Enter age: ");	// nhap tuoi
			int age = Integer.parseInt(scan.nextLine());	// tranh troi lenh nhu khi dung scan.nextInt()
			student[i] = new Student(name, age);	// khoi tao doi tuong Student thu i
		}

		scan.close();
		return student;
	}

	public void write(Student[] student) {	//ghi theo Object
		try {	// dat try cacth de tranh ngoai le khi tao va ghi File
			FileOutputStream f = new FileOutputStream("student.dat");	// tao file f tro den student.dat
			ObjectOutputStream oStream = new ObjectOutputStream(f);	// dung de ghi theo Object vao file f
			oStream.writeObject(student);	// ghi student theo kieu Object vao file
			oStream.close();
		} catch (IOException e) {
			System.out.println("Error Write file");
		}
	}

	public Student[] read() {		// doc theo Object
		Student[] student = null;
		try {	// dat try cacth de tranh ngoai le khi tao va doc File
			FileInputStream f = new FileInputStream("student.dat");	// tao file f tro den student.dat
			ObjectInputStream inStream = new ObjectInputStream(f);	// dung de doc theo Object vao file f
			// dung inStream doc theo Object, ep kieu tra ve la Student
			student = (Student[]) inStream.readObject();
			inStream.close();
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");
		} catch (IOException e) {
			System.out.println("Error Read file");
		}
		return student;
	}

	public void show(Student[] student) throws Exception {		//In data doc duoc tu file ra man hinh
		try {
			for (int i = 0; i < student.length; i++) {
				System.out.println((i + 1) + " : My name is "
						+ student[i].getName() + ". I am "
						+ student[i].getAge() + " years old");
			}
		} catch (NullPointerException e) {
			System.out.println("File Empty");
		}
	}
}

続きを読む: Javaでファイルにオブジェクトを追加することを忘れないでください