上一篇中,介绍了RandomAccessFile类,RandomAccessFile类的API,以及通过RandomAccessFile对文件进行增删改查(改没完成)
同时发现了借助数据结构我们能更好的操作文件,把对文件的操作转换成对集合(ArrayList)的操作。把对文件的增删改查转换成对集合(ArrayList)的增删改查。这样就比较简单了。
这里来介绍修改的方法。查询的话直接查询ArrayList即可
学生类:
import java.io.IOException;
import java.io.RandomAccessFile;
public class Student
{
String id;// 学号
String name;// 名字
String sex;// 性别
int age;// 年龄
public Student()
{
}
public Student(String id, String name, String sex, int age)
{
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeUTF(this.id);
raf.writeUTF(this.name);
raf.writeUTF(this.sex);
raf.writeInt(this.age);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
this.id = raf.readUTF();
this.name = raf.readUTF();
this.sex = raf.readUTF();
this.age = raf.readInt();
}
@Override
public String toString()
{
return "Student [id=" + id + ", name=" + name + ", sex=" + sex
+ ", age=" + age + "]";
}
/**
* 获取到第n条学生记录
*/
public Student getTheNthStudent(RandomAccessFile raf, int n)
throws IOException
{
if (n < 1)
{
System.out.println("n要大于1");
return null;
}
Student student = new Student();
raf.seek(0);
n--;
while (n >= 0)
{
student.readStudent(raf);// 读取一条记录
n--;
}
return student;
}
}
工具类:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
//文件列表
public class StudentArrayList
{
static ArrayList<Student> stuList=new ArrayList<Student>();
public static void initializeList() throws IOException
{
File file =new File(".\\text2.txt");
//以只读的方式打开文件
RandomAccessFile raf=new RandomAccessFile(file,"r");
while(raf.getFilePointer()<raf.length())
{
Student student=new Student();
student.readStudent(raf);//从文件中读取一条记录
stuList.add(student);//把这条记录生成的对象添加到集合中去。
}
raf.close();//关闭文件
}
public static void writeList(RandomAccessFile raf) throws IOException
{
//把集合中的所有Student对象写入到文件中
for (Student student : stuList)
{
student.writeStudent(raf);
}
}
//显示List中的所有学生
public static void showAllStudent()
{
for (Student student : stuList)
{
System.out.println(student);
}
}
public static void writeChange() throws IOException
{
RandomAccessFile raf=new RandomAccessFile(new File(".\\text2.txt"), "rw");
raf.setLength(0);
writeList(raf);
raf.close();
}
}
测试:
import java.io.IOException;
public class ModifyRAF
{
public static void main(String[] args) throws IOException
{
StudentArrayList.initializeList();//初始化ArrayList
StudentArrayList.showAllStudent();
Student student = new Student();
//获取第2条记录
student = StudentArrayList.stuList.get(2-1);
student.id="B2000";
student.name="小红";
student.sex="女";
student.age=21;
StudentArrayList.stuList.set(2-1, student);
System.out.println("--------------------------------");
StudentArrayList.showAllStudent();
StudentArrayList.writeChange();//更新记录文件
}
}
运行结果:
Student [id=B10005, name=小蓝, sex=男, age=19]
Student [id=B10005, name=小蓝, sex=男, age=19]
Student [id=B1000, name=小明名名, sex=男, age=20]
Student [id=B1001, name=小黄, sex=男, age=20]
--------------------------------
Student [id=B10005, name=小蓝, sex=男, age=19]
Student [id=B2000, name=小红, sex=女, age=21]
Student [id=B1000, name=小明名名, sex=男, age=20]
Student [id=B1001, name=小黄, sex=男, age=20]