基本信息
文件名称:《Java语言程序设计》第6章 流-教学课件(非AI生成).ppt
文件大小:433 KB
总页数:35 页
更新时间:2025-05-16
总字数:约9.18千字
文档摘要

RandomAccessFile类构造方法。RandomAccessFile(Stringname,Stringmode)参数name用来确定一个文件名,给出流的源,同时也是流目的地。参数mode取r(只读)或rw(可读写)决定流对文件的访问权限。RandomAccessFile(Filefile,Stringmode)参数file是一个File对象,给出流的源,同时也是流目的地。参数mode取r(只读)或rw(可读写)决定流对文件的访问权限。*RandomAccessFile类Filefile=newFile(args[0]);//建立RandomAccessFile实例并以读写模式开启文件RandomAccessFilerandomAccessFile=newRandomAccessFile(file,rw);for(inti=0;istudents.length;i++){//使用对应的write方法写入数据randomAccessFile.writeChars(students[i].getName());randomAccessFile.writeInt(students[i].getScore());}*RandomAccessFile类//使用seek()方法操作存取位置randomAccessFile.seek((num-1)*Student.size());Studentstudent=newStudent();//使用对应的read方法读出数据student.setName(readName(randomAccessFile));student.setScore(randomAccessFile.readInt());System.out.println(姓名:+student.getName());System.out.println(分数:+student.getScore());//设定关闭文件randomAccessFile.close();*RandomAccessFile类privatestaticStringreadName(RandomAccessFile randomAccessfile)throwsIOException{char[]name=newchar[15];for(inti=0;iname.length;i++)name[i]=randomAccessfile.readChar();//将空字符取代为结束符并传回处理结果returnnewString(name).replace(\0,);}*把输入流的指向称做源,程序从指向源的输入流中读取源中的数据。而输出流的指向是字节要去的一个目的地(或用户),程序通过向输出流中写入数据把信息传递到目的地。第6章流1File类文件是相关记录或放在一起的数据的集合.Java程序一般通过java.io.File类对象,获取文件本身的一些信息,例如文件所在的目录、文件的长度、文件读写权限等,不涉及对文件的读写操作。*1File类的构造方法File(Stringpathname)Filef=newFile(c:\\data\\temp.dat”);Filef=newFile(data\\temp.dat”);Filef=newFile(temp.dat”);File(Stringparent,Stringchild)Filef=newFile(c:\\data,temp.dat);Filef=newFile(data,temp.dat);File(Fileparent,Stringchild)Filef=newFile(newFile(c:\\d