📄 studentfile.java
字号:
//【例9.4】 将若干学生对象写入一个对象文件中。
import java.io.*;
public class StudentFile
{
private String filename;
public StudentFile(String filename)
{
this.filename = filename;
}
public void writeToFile() throws IOException //向指定文件写入若干学生对象
{
FileOutputStream fout = new FileOutputStream(this.filename);
ObjectOutputStream objout = new ObjectOutputStream(fout);
objout.writeObject(new Student("杨柳")); //写入一个对象
objout.writeObject(new Student("明明"));
objout.writeObject(new Student("小玲"));
objout.close(); //先关闭对象流
fout.close(); //再关闭文件流
System.out.println("Write Student to File "+this.filename);
}
public void readFromFile() throws IOException //从指定文件中读取若干学生对象
{
FileInputStream fin = new FileInputStream(this.filename);
ObjectInputStream objin = new ObjectInputStream(fin);
System.out.println("readFromFile "+this.filename);
while (true) //输入流未结束时
try
{
Student stu = (Student)objin.readObject(); //读取一个对象
System.out.println(stu.toString()+" ");
}
catch (Exception e)
{
break;
}
objin.close(); //先关闭对象流
fin.close(); //再关闭文件流
}
public Student[] openFile() throws IOException //返回从指定文件中读取的学生对象数组
{
FileInputStream fin = new FileInputStream(this.filename);
ObjectInputStream objin = new ObjectInputStream(fin);
Student[] students = new Student[20];
int i=0;
while (true)
try
{
students[i] = (Student)objin.readObject(); //读取一个对象
i++;
}
catch (Exception e) //捕获IOException和ClassNotFoundException异常
{
break;
}
objin.close(); //先关闭对象流
fin.close(); //再关闭文件流
return students;
}
public static void main(String args[]) throws IOException
{
StudentFile afile = new StudentFile("StudentFile.dat");
afile.writeToFile();
afile.readFromFile();
}
}
/*
程序运行结果如下:
Write Fibonacci to File StudentFile.dat
readFromFile StudentFile.dat
1 杨柳
2 明明
3 小玲
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -