Java文件读写

来源:互联网 发布:网络节点的度 编辑:程序博客网 时间:2024/06/05 13:33

读写一个班级人员的信息,包括学号,姓名,年龄成绩。


//主类 读写
package text_Example;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import java.util.Iterator;import java.util.LinkedList;import java.util.List;public class TestRandom {public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("F:\\JAVA\\lpf.JAVA\\Text_File\\16140112.txt");RandomAccessFile raf=null;Student stu;List<Student> list=new LinkedList<Student>();list.add(new Student(1614011101,"李四",20,new double[]{99,55,77}));list.add(new Student(1614011102,"王老五",21,new double[]{98,75,97}));list.add(new Student(1614011103,"赵二",19,new double[]{92,56,78}));list.add(new Student(1614011104,"张三",20,new double[]{99,80,77}));Iterator ite=list.iterator();byte[] name =new byte[12];byte[] nametemp;double score[]=new double[3];//一下开始写入try{raf=new RandomAccessFile(file,"rw");while(ite.hasNext()){stu=(Student) ite.next();raf.writeInt(stu.getNumber());nametemp=stu.getName().getBytes();//将字符串形式转化为Byte数组形式;System.arraycopy(nametemp,0,name,0,nametemp.length);/*将Byte数组name拷贝给nametemp数组,方法System.arraycopy(要拷贝的数组,要拷贝的起始位置,目标数组,目标数组接收的位置,拷贝长度);*/raf.write(name);raf.writeInt(stu.getAge());score=stu.getScore();for(int i=0;i<score.length;i++){raf.writeDouble(score[i]);}}}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}finally{try{if(raf!=null)//如果文件不为空 关闭raf.close();}catch(IOException e){e.printStackTrace();}}readStudent(file);/*readStudent(file);是依次读出数据的方法 *searchStudent(file,3);是读取目标文件的方法*/}//下面开始依次读出文件private static void readStudent(File file) {// TODO Auto-generated method stubRandomAccessFile raf=null;Student stu;long length;long position=0;//记录数据位置int number;String name;byte[] nametemp=new byte[12];int age;double score []=new double[3];try {raf=new RandomAccessFile(file,"r");raf.seek(0);//找到要读取数据的起始位置length=raf.length();while(position<length){number=raf.readInt();raf.readFully(nametemp);//读出Byte的方法name=new String(nametemp,0,nametemp.length);age=raf.readInt();for(int i=0;i<score.length;i++){score[i]=raf.readDouble();}stu=new Student(number,name.trim(),age,score);//将读取的数据传给Student对象stu.printStudent();//打印数据position=raf.getFilePointer();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try{if(raf!=null)raf.close();}catch(IOException e){e.printStackTrace();}}}//下面开始读取目标文件public static void searchStudent(File file,int num){RandomAccessFile raf=null;Student stu;long length;long position=0;int number;String name;byte[] nametemp=new byte[12];int age;double score[]=new double[3];try {raf=new RandomAccessFile(file,"r");raf.seek((num-1)*44);//找到要读取数据的起始位置/* * 数据开头位置为 (要读取数据组数-1)*一组数据大小 */number=raf.readInt();raf.readFully(nametemp);name=new String(nametemp,0,nametemp.length);age=raf.readInt();for(int i=0;i<score.length;i++){score[i]=raf.readDouble();}stu=new Student(number,name.trim(),age,score);stu.printStudent();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


}
//学生类
package text_Example;import java.util.Arrays;public class Student {private int number;private String name;private int age;private double score[];public Student(int number, String name, int age, double[] score) {super();this.number = number;this.name = name;this.age = age;this.score = score;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double[] getScore() {return score;}public void setScore(double[] score) {this.score = score;}public int compareTo(Object o){return this.age-((Student)o).getAge();}public String toString(){return name+"的年龄是:"+age;}public void printStudent(){System.out.println(name+"的学号是:"+number+",年龄是:"+age+",各科成绩是"+Arrays.toString(score));}}

                                             
0 0
原创粉丝点击