Java IO流 编写程序,从控制台输入多个学生信息写入本地文件,并且读取打印出来
题目:编写程序,从控制台输入多个学生信息写入本地文件,并且读取打印出来。
题目主要考察IO流相关知识,这里需要用到ObjectInputStream和ObjectOutputStream两个和对象相关的流。由于学生个数是不确定的,所以需要使用到ArrayList集合来存放对象,然后把集合存放到文件中,读取操作相同。
输出信息要进行验证,这里考虑的并不周全,只有分数验证。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
// 操作的文件路径
static File file = new File("d:\\data.dat");
public static void main(String[] args) {
write();
read();
}
// 把对象写入文件方法
public static void write() {
ObjectOutputStream objectOutputStream = null;
Scanner scanner = new Scanner(System.in);
// 学生信息
String name = null;
String sex = null;
String grade = null;
// 集合保存学生对象
ArrayList<Object> arrayList = new ArrayList<Object>();
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
// 输入学生对象信息
while (true) {
Student student = new Student();
System.out.print("请输入姓名:");
name = scanner.nextLine();
System.out.print("请输入性别:");
sex = scanner.nextLine();
System.out.print("请输入分数:");
// 分数判断
while (true) {
// 如果获取的不是数字就重新输入
// 如果是数字判断数字范围是否在0-100中间
if (scanner.hasNextInt()) {
grade = scanner.nextLine();
if (Integer.parseInt(grade) < 0) {
System.out.print("分数不能小于0,请重新输入分数:");
} else if (Integer.parseInt(grade) > 100) {
System.out.print("分数不能大于100,请重新输入分数:");
} else {
break;
}
} else {
grade = scanner.nextLine();
System.out.print("格式错误,请重新输入分数:");
}
}
student.setName(name);
student.setSex(sex);
student.setGrade(Integer.parseInt(grade));
arrayList.add(student);
System.out.print("是否退出(Y/N):");
String str = scanner.nextLine();
if ("Y".equalsIgnoreCase(str)) {
break;
}
}
// 将集合对象写入文件
objectOutputStream.writeObject(arrayList);
// 刷新
objectOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 读取文件对象方法
public static void read() {
try {
// 读取文件对象类
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
// 对象放入arraylist
ArrayList<Object> arrayList = (ArrayList<Object>) objectInputStream.readObject();
System.out.println("----" + "读取的学生信息如下" + "----");
// 遍历所有学生对象
for (int i = 0; i < arrayList.size(); i++) {
System.out.print("第" + (i + 1) + "个学生信息为:");
// 向下转型,判断类型
if (arrayList.get(i) instanceof Student) {
Student student = (Student) arrayList.get(i);
System.out.print("姓名:" + student.getName());
System.out.print(",性别:" + student.getSex());
System.out.print(",成绩:" + student.getGrade() + "\n");
}
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
student类
public class Student implements Serializable {
private String name;
private String sex;
private int grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
运行结果