实验目的
掌握I/O程序设计,能够读写本地文件等
实验要求
(1) 编写一个程序,如果文件Exercisel_01.txt 不存在,就创建一个名为Exercisel_01.txt 的文件。向这个文件追加新数据。使用文本I/O将20个随机生成的整数写入这个文件。文件中的整数用空格分隔。(20分)
(2) 编写一个程序,如果文件Exercisel_02.dat 不存在,就创建一个名为Exercisel_02.dat 的文件。向这个文件追加新数据。使用二进制I/O 将20个随机生成的整数写入这个文件中。利用二进制I/O读取这个文件中的内容并显示。(20分)
(3) 对一个学生成绩单进行读写操作。请直接下载blackboard上提供的data.txt文件作为数据源。具体要求如下:
新建一个可序列化的Student类,其中包括四个成员变量:int型学号、String类型学生名字、String类型学生所在学院、int型成绩(参考data.txt文件)。重写toString方法用于打印Student对象。(10分)
使用BufferedReader从data.txt文件中读取数据,并存放到一个集合对象中,要求按照学生名字的字母顺序升序排列。(10分)创建一个本地文件output.txt,将集合中的数据序列化到此文件中。(10分)
将output.txt中的数据反序列化,按照降序输出所有成绩在92分以上的学生信息;如果学生成绩相同,则按照学生名字的字母顺序降序输出。(10分)
实验步骤
1. 编写一个程序,如果文件Exercisel_01.txt 不存在,就创建一个名为Exercisel_01.txt 的文件。向这个文件追加新数据。使用文本I/O将20个随机生成的整数写入这个文件。文件中的整数用空格分隔。(20分)
package HW9;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
//本包HW9的文件地址:"D:\IntelliJ IDEA 2023.2.5\project_1\Algorithm\src\HW9\"
public class Q1 {
static void input(String str)throws IOException
{
FileOutputStream f=new FileOutputStream(str,true); //append:在原有基础上添加内容
Random r=new Random();
for(int i=1;i<=20;i++) //记录次数,写入20次
{
int a=r.nextInt(100);//获取随机整数,限制随机数为100以内
String c=String.valueOf(a);//将整数转为String型,将其写入文件,避免乱码
f.write(c.getBytes()); //写入整数
f.write(" ".getBytes()); //写入每个整数的分隔符
if(i%5==0)
f.write('\n'); //每输入5个数换行
}
f.close(); //资源释放
}
public static void main(String []args) throws IOException {
String str="D:\\IntelliJ IDEA 2023.2.5\\project_1\\Algorithm\\src\\HW9\\Exercisel_01.txt";
input(str);
}
}
2. 编写一个程序,如果文件Exercisel_02.dat 不存在,就创建一个名为Exercisel_02.dat 的文件。向这个文件追加新数据。使用二进制I/O 将20个随机生成的整数写入这个文件中。利用二进制I/O读取这个文件中的内容并显示。
package HW9;
import java.io.*;
import static HW9.Q1.*; //导入Q1中的input方法
//本包HW9的文件地址:"D:\IntelliJ IDEA 2023.2.5\project_1\Algorithm\src\HW9\"
public class Q2 {
static void output(String s) throws IOException {
//字节输入流
FileInputStream f = new FileInputStream(s);
//读取方法一:逐步读取法,一个一个字符进行读取
// int a;
// while((a=f.read())!=-1)
// {
// System.out.print((char)a);
// }
//方法二:一次性读取,储存在字符数组中
byte []b=new byte[10000];
int t=f.read(b);
System.out.println(new String(b,0,t));
f.close();
}
public static void main(String []args) throws IOException {
String str="D:\\IntelliJ IDEA 2023.2.5\\project_1\\Algorithm\\src\\HW9\\Exercisel_02.txt";
input(str);
output(str);
}
}
3. 对一个学生成绩单进行读写操作。
package HW9;
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
//本包HW9的文件地址:"D:\IntelliJ IDEA 2023.2.5\project_1\Algorithm\src\HW9\"
//可序列化对象类Student,继承接口
class Student implements Serializable {
int id; //学号
String name; //姓名
String college;//学院
int score; //成绩
//构造方法
Student(int i, String n, String c, int s) {
id = i;
name = n;
college = c;
score = s;
}
//重写toString方法
@Override
public String toString() {
//System.out.println("id: " + id + " name: " + name + " college: " + college + " score: " + score);
return "id: " + id + " name: " + name + " college: " + college + " score: " + score;
}
}
public class Q3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建ArrayList对象,收集数据
List<Student> stu = new ArrayList<>();
String str1 = "D:\\IntelliJ IDEA 2023.2.5\\project_1\\Algorithm\\src\\HW9\\data.txt";//本机地址
BufferedReader reader = new BufferedReader(new FileReader(str1));
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(" "); //分隔出对应的数据
int id = Integer.parseInt(data[0].trim()); //学号
String n = data[1].trim(); //姓名
String c = data[2].trim();//学院
int s = Integer.parseInt(data[3].trim()); //成绩
Student student = new Student(id, n, c, s);
stu.add(student);
}
//对收集到的数据进行排序
//排序规则:按照学生名字的字母顺序升序排列
//本题通过实现comparator的重写来实现此算法
stu.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name); //按照学生姓名进行升序排序
}
});
//序列化至output.txt
String str2 = "D:\\IntelliJ IDEA 2023.2.5\\project_1\\Algorithm\\src\\HW9\\output.txt";
// BufferedWriter f2 = new BufferedWriter(new FileWriter(str2));
// for (int i = 0; i < len; i++) {
// f2.write(stu.get(i).toString());
// f2.newLine(); //每输入一组,执行换行代码
// }
// f2.close(); //资源释放
//序列化至output.tx
ObjectOutputStream f2 = new ObjectOutputStream(new FileOutputStream(str2)); //新建文本文件
f2.writeObject(stu); //序列化写入
f2.close(); //资源释放
// 反序列化output.txt文件数据并输出成绩在92分以上的学生信息
ObjectInputStream f3 = new ObjectInputStream(new FileInputStream(str2));
List<Student> list = (List<Student>) f3.readObject();
//对收集到的数据进行排序
//排序规则:优先按照学生成绩进行升序排序,如果学生成绩相同,则按照学生名字的字母顺序降序输出
//本题通过实现comparator的重写来实现此算法
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.score != o2.score)
return o1.score - o2.score; //当成绩不相等时,按照成绩升序排序
else
return -o1.name.compareTo(o2.name); //成绩相等时,按照名字降序排序
}
});
for (Student student : list) {
if (student.score > 92) { //当成绩大于92时,直接输出Student的全部内容
System.out.println(student);
}
}
f3.close();//资源释放
}
}
实验结论
在本次实验中,通过亲手操作,让我对文本的输入输出有了一个更加深入的了解,而这些,也是日常工作中会常用到的一些东西,通过此次实验,锻炼了我的动手及思维能力,也增强了我的实际工作能力。