RandomAccessFile类另 File file =new File(args [0] //建立 Randomaccessfile实例并以读写模式开后档案 Randomaccess file randomaccessFile new RandomAccessFile(file,"rw") for (int i=0; i< students. length; i++) //使用对应的 write方法写人数据 randomAccessFile writechars(students[i]. getName())i randomAccessFile. writeInt (students[i]. getscore())i
RandomAccessFile类别 File file = new File(args[0]); //建立RandomAccessFile实例并以读写模式开启档案 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); for(int i = 0; i < students.length; i++) { //使用对应的write方法写入数据 randomAccessFile.writeChars(students[i].getName()); randomAccessFile.writeInt(students[i].getScore()); }
RandomAccessFile类别 //使用sek()方法操作存取位置 randomAccessfile seek((num-1)* Student size)i Student student new Student ( //使用对应的read方法读出数据 student setName(readName(randomAccessfile))i student. setscore(randomAccessFile readInt() System. out. println("*E: " student getName())i System. out. println("*:"+ student. getscore())i //设定矣闭档案 randomAccessFile close()i
RandomAccessFile类别 //使用seek()方法操作存取位置 randomAccessFile.seek((num-1) * Student.size()); Student student = new Student(); //使用对应的read方法读出数据 student.setName(readName(randomAccessFile)); student.setScore(randomAccessFile.readInt()); System.out.println("姓名:" + student.getName()); System.out.println("分数:" + student.getScore()); //设定关闭档案 randomAccessFile.close();
RandomAccessFile类另 private static String readName(RandomAccessFilerandomAccessfile throws IOException char[ name new char [l5] for(int i=0; i< namelength; i++) name [i]= randomAccessfile readchar()i //捋空字符取代为空格符并传回 return new String(name).replace('\0,Ii)i
RandomAccessFile类别 private static String readName(RandomAccessFile randomAccessfile) throws IOException { char[] name = new char[15]; for(int i = 0; i < name.length; i++) name[i] = randomAccessfile.readChar(); //将空字符取代为空格符并传回 return new String(name).replace('\0', ' '); }
RandomAccessFile类另 读写档案时几个必要的流程 开后档案并指定读写方式 一使用对应的写入方法 使用对应的读出方法 尖闭档案
RandomAccessFile类别 • 读写档案时几个必要的流程 – 开启档案并指定读写方式 – 使用对应的写入方法 – 使用对应的读出方法 – 关闭档案
Inputstream Outputstream 数据流动抽象化为一个串流( Strean) 檔案 HuLC 體 0 00
InputStream、OutputStream • 数据流动抽象化为一个串流(Stream)