将Student对象(属性:int id, String name(不多于10个字符),int age,double grade)写入文件、读出文件,分别尝试使用以下方式实现: (1)DataInputStream, DataOutputStream; (2)Scanner(或BufferedReader), PrintWriter; (3)ObjectInputStream, ObjectOutputStream。 问题:1、2、3方法生成的文件大小各是多少?尝试分析1和2输出的文件大小。 要求:以方法的形式进行封装(使用方法重载),例如: public static void writeStudent(Student s, DataOutputStream dos){}; 或者:public static void writeStudent(List stus, DataOutputStream dos){}; 读取时类似: public static Student readStudent(DataInputStream dis){}; 或者:public static List readStudent(DataInputStream dis){};
时间: 2024-01-06 14:03:14 浏览: 80
以下是实现以上要求的代码:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Student {
private int id;
private String name;
private int age;
private double grade;
public Student(int id, String name, int age, double grade) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getGrade() {
return grade;
}
public static void writeStudent(Student s, DataOutputStream dos) throws IOException {
dos.writeInt(s.getId());
dos.writeUTF(s.getName());
dos.writeInt(s.getAge());
dos.writeDouble(s.getGrade());
}
public static void writeStudent(List<Student> stus, DataOutputStream dos) throws IOException {
dos.writeInt(stus.size());
for (Student s : stus) {
writeStudent(s, dos);
}
}
public static Student readStudent(DataInputStream dis) throws IOException {
int id = dis.readInt();
String name = dis.readUTF();
int age = dis.readInt();
double grade = dis.readDouble();
return new Student(id, name, age, grade);
}
public static List<Student> readStudents(DataInputStream dis) throws IOException {
List<Student> stus = new ArrayList<>();
int num = dis.readInt();
for (int i = 0; i < num; i++) {
stus.add(readStudent(dis));
}
return stus;
}
public static void writeStudent(Student s, PrintWriter pw) {
pw.println(s.getId());
pw.println(s.getName());
pw.println(s.getAge());
pw.println(s.getGrade());
}
public static void writeStudent(List<Student> stus, PrintWriter pw) {
pw.println(stus.size());
for (Student s : stus) {
writeStudent(s, pw);
}
}
public static Student readStudent(Scanner scanner) {
int id = scanner.nextInt();
String name = scanner.next();
int age = scanner.nextInt();
double grade = scanner.nextDouble();
return new Student(id, name, age, grade);
}
public static List<Student> readStudents(Scanner scanner) {
List<Student> stus = new ArrayList<>();
int num = scanner.nextInt();
for (int i = 0; i < num; i++) {
stus.add(readStudent(scanner));
}
return stus;
}
public static void writeStudent(Student s, ObjectOutputStream oos) throws IOException {
oos.writeObject(s);
}
public static void writeStudent(List<Student> stus, ObjectOutputStream oos) throws IOException {
oos.writeObject(stus);
}
public static Student readStudent(ObjectInputStream ois) throws IOException, ClassNotFoundException {
return (Student) ois.readObject();
}
public static List<Student> readStudents(ObjectInputStream ois) throws IOException, ClassNotFoundException {
return (List<Student>) ois.readObject();
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<Student> stus = new ArrayList<>();
stus.add(new Student(1, "Tom", 18, 90));
stus.add(new Student(2, "Jerry", 19, 80));
stus.add(new Student(3, "Mike", 20, 70));
// 使用DataInputStream, DataOutputStream
FileOutputStream fos1 = new FileOutputStream("data1.txt");
DataOutputStream dos1 = new DataOutputStream(fos1);
writeStudent(stus, dos1);
dos1.close();
FileInputStream fis1 = new FileInputStream("data1.txt");
DataInputStream dis1 = new DataInputStream(fis1);
List<Student> stus1 = readStudents(dis1);
dis1.close();
// 使用Scanner(或BufferedReader), PrintWriter
FileOutputStream fos2 = new FileOutputStream("data2.txt");
PrintWriter pw2 = new PrintWriter(fos2);
writeStudent(stus, pw2);
pw2.close();
FileInputStream fis2 = new FileInputStream("data2.txt");
Scanner scanner2 = new Scanner(fis2);
List<Student> stus2 = readStudents(scanner2);
scanner2.close();
// 使用ObjectInputStream, ObjectOutputStream
FileOutputStream fos3 = new FileOutputStream("data3.txt");
ObjectOutputStream oos3 = new ObjectOutputStream(fos3);
writeStudent(stus, oos3);
oos3.close();
FileInputStream fis3 = new FileInputStream("data3.txt");
ObjectInputStream ois3 = new ObjectInputStream(fis3);
List<Student> stus3 = readStudents(ois3);
ois3.close();
System.out.println("stus1: " + stus1);
System.out.println("stus2: " + stus2);
System.out.println("stus3: " + stus3);
File file1 = new File("data1.txt");
System.out.println("data1.txt size: " + file1.length() + " bytes");
File file2 = new File("data2.txt");
System.out.println("data2.txt size: " + file2.length() + " bytes");
File file3 = new File("data3.txt");
System.out.println("data3.txt size: " + file3.length() + " bytes");
}
}
```
上述代码中,使用了方法重载的形式,可以根据需要选择不同的方法进行读写操作。在main方法中,我们首先创建了一个包含三个学生的List对象stus,并分别使用DataInputStream、Scanner和ObjectInputStream进行写入文件操作,然后再使用相应的读取方式将文件中的数据读取出来并存入新的List对象中。最后,我们输出了三个List对象以及生成的三个文件的大小。需要注意的是,在使用DataInputStream和PrintWriter进行读写操作时,需要使用相应的流对象的close方法关闭流,而在使用ObjectInputStream和ObjectOutputStream进行读写操作时,不需要手动关闭流对象,因为它们会在读写完成后自动关闭。
阅读全文