将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 21:42:19 浏览: 30
方法一:DataInputStream, DataOutputStream
写入文件代码如下:
```java
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 writeStudents(List<Student> stus, DataOutputStream dos) throws IOException {
dos.writeInt(stus.size());
for (Student s : stus) {
writeStudent(s, dos);
}
}
```
读取文件代码如下:
```java
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 {
int size = dis.readInt();
List<Student> stus = new ArrayList<>();
for (int i = 0; i < size; i++) {
stus.add(readStudent(dis));
}
return stus;
}
```
方法二:Scanner, PrintWriter
写入文件代码如下:
```java
public static void writeStudent(Student s, PrintWriter pw) {
pw.println(s.getId() + " " + s.getName() + " " + s.getAge() + " " + s.getGrade());
}
public static void writeStudents(List<Student> stus, PrintWriter pw) {
for (Student s : stus) {
writeStudent(s, pw);
}
}
```
读取文件代码如下:
```java
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<>();
while (scanner.hasNext()) {
stus.add(readStudent(scanner));
}
return stus;
}
```
方法三:ObjectInputStream, ObjectOutputStream
写入文件代码如下:
```java
public static void writeStudent(Student s, ObjectOutputStream oos) throws IOException {
oos.writeObject(s);
}
public static void writeStudents(List<Student> stus, ObjectOutputStream oos) throws IOException {
for (Student s : stus) {
writeStudent(s, oos);
}
}
```
读取文件代码如下:
```java
public static Student readStudent(ObjectInputStream ois) throws IOException, ClassNotFoundException {
return (Student) ois.readObject();
}
public static List<Student> readStudents(ObjectInputStream ois) throws IOException, ClassNotFoundException {
List<Student> stus = new ArrayList<>();
while (true) {
try {
stus.add(readStudent(ois));
} catch (EOFException e) {
break;
}
}
return stus;
}
```
对于问题一,可以通过以下代码来获取生成的文件大小:
```java
File file = new File("students.dat");
System.out.println(file.length());
```
对于问题二,我们知道DataOutputStream和Scanner是直接写入/读出基本类型或字符串,而PrintWriter则是将数据转化为字符串后再写入文件,因此DataOutputStream和Scanner生成的文件大小相对较小,而PrintWriter生成的文件大小相对较大。
完整代码如下:
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)