题目内容
/*** 往文件中依次写入3名员工的信息,* 每位员工有工龄和姓名两个字段 然后按照* 第二名,第一名,第三名的先后顺序读取员工信息*/import java.io.File;import java.io.RandomAccessFile;public class RandomAccessFileTest {public static void main(String[] args) throws Exception {Employee employee1 = new Employee(5, "张三");Employee employee2 = new Employee(6, "lisi");Employee employee3 = new Employee(7, "James");File file = new File("employee.txt");if(!file.exists()){(1) //如果文件不存在,则新建文件}//设置文件的打开方式为读写RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");randomAccessFile.writeChars(employee1.getName());//写出第一个员工的姓名randomAccessFile.writeInt(employee1.getWork_age());//写出第一个员工的工龄randomAccessFile.writeChars(employee2.getName());randomAccessFile.writeInt(employee2.getWork_age());randomAccessFile.writeChars(employee3.getName());randomAccessFile.writeInt(employee3.getWork_age());randomAccessFile.close();//关闭文件RandomAccessFile raf2 =(2)//以只读方式打开文件(3)//跳过第一个员工String strName2 = "";for(int i=0;i LEN){name = name.substring(0, LEN);}else{while(name.length() < LEN){name = name + "$";}}this.name = name;}public int getWork_age() {return work_age;}public String getName() {return name;}}
查看答案
搜索结果不匹配?点我反馈
更多问题