[Java的] 读出记录在Java对象 – 阅读在Java中写入对象

在其中,我们需要沟通的对象类读取记录在Java对象 了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 学生类属性包含姓名和年龄和get方法, 设定值的. 一ProcessStudent类包括方法,允许学生从键盘输入, 记录在学生文件, 由Object类型,从文件中读取,最后显示读取信息. 在代码中很清楚地解释:

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中的文件