将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){};
时间: 2023-12-03 16:42:33 浏览: 86
读取student.txt文件内容,每行作为一条数据,创建对应Student对象。
以下是三种方式的实现,以及方法封装:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentFile {
// 使用 DataOutputStream 写入单个 Student 对象
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());
dos.flush();
}
// 使用 DataOutputStream 写入多个 Student 对象
public static void writeStudent(List<Student> stus, DataOutputStream dos) throws IOException {
dos.writeInt(stus.size());
for (Student s : stus) {
writeStudent(s, dos);
}
dos.flush();
}
// 使用 DataInputStream 读取单个 Student 对象
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);
}
// 使用 DataInputStream 读取多个 Student 对象
public static List<Student> readStudents(DataInputStream dis) throws IOException {
int n = dis.readInt();
List<Student> stus = new ArrayList<>();
for (int i = 0; i < n; i++) {
stus.add(readStudent(dis));
}
return stus;
}
// 使用 PrintWriter 写入单个 Student 对象
public static void writeStudent(Student s, PrintWriter pw) {
pw.println(s.getId());
pw.println(s.getName());
pw.println(s.getAge());
pw.println(s.getGrade());
pw.flush();
}
// 使用 PrintWriter 写入多个 Student 对象
public static void writeStudent(List<Student> stus, PrintWriter pw) {
pw.println(stus.size());
for (Student s : stus) {
writeStudent(s, pw);
}
pw.flush();
}
// 使用 Scanner 读取单个 Student 对象
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);
}
// 使用 Scanner 读取多个 Student 对象
public static List<Student> readStudents(Scanner scanner) {
int n = scanner.nextInt();
List<Student> stus = new ArrayList<>();
for (int i = 0; i < n; i++) {
stus.add(readStudent(scanner));
}
return stus;
}
// 使用 ObjectOutputStream 写入多个 Student 对象
public static void writeStudent(List<Student> stus, ObjectOutputStream oos) throws IOException {
oos.writeObject(stus);
oos.flush();
}
// 使用 ObjectInputStream 读取多个 Student 对象
@SuppressWarnings("unchecked")
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.5));
stus.add(new Student(2, "Jerry", 19, 88.5));
stus.add(new Student(3, "Bob", 20, 85.0));
// 使用 DataOutputStream 写入多个 Student 对象
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
DataOutputStream dos1 = new DataOutputStream(baos1);
writeStudent(stus, dos1);
System.out.println("使用 DataOutputStream 写入多个 Student 对象,文件大小:" + baos1.size() + " 字节");
// 使用 PrintWriter 写入多个 Student 对象
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
PrintWriter pw2 = new PrintWriter(new OutputStreamWriter(baos2));
writeStudent(stus, pw2);
System.out.println("使用 PrintWriter 写入多个 Student 对象,文件大小:" + baos2.size() + " 字节");
// 使用 ObjectOutputStream 写入多个 Student 对象
ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
ObjectOutputStream oos3 = new ObjectOutputStream(baos3);
writeStudent(stus, oos3);
System.out.println("使用 ObjectOutputStream 写入多个 Student 对象,文件大小:" + baos3.size() + " 字节");
// 使用 DataInputStream 读取多个 Student 对象
ByteArrayInputStream bais1 = new ByteArrayInputStream(baos1.toByteArray());
DataInputStream dis1 = new DataInputStream(bais1);
List<Student> stus1 = readStudents(dis1);
System.out.println("使用 DataInputStream 读取多个 Student 对象:" + stus1);
// 使用 Scanner 读取多个 Student 对象
ByteArrayInputStream bais2 = new ByteArrayInputStream(baos2.toByteArray());
Scanner scanner2 = new Scanner(new InputStreamReader(bais2));
List<Student> stus2 = readStudents(scanner2);
System.out.println("使用 Scanner 读取多个 Student 对象:" + stus2);
// 使用 ObjectInputStream 读取多个 Student 对象
ByteArrayInputStream bais3 = new ByteArrayInputStream(baos3.toByteArray());
ObjectInputStream ois3 = new ObjectInputStream(bais3);
List<Student> stus3 = readStudents(ois3);
System.out.println("使用 ObjectInputStream 读取多个 Student 对象:" + stus3);
}
}
```
阅读全文