RandomAccessFile使用
RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了。这些记录的大小不必相同;但是其大小和位置必须是可知的。但是该类仅限于操作文件。
RandomAccessFile不属于InputStream和OutputStream类系的。实际上,除了实现DataInput和DataOutput接口之外(DataInputStream和DataOutputStream也实现了这两个接口),它和这两个类系毫不相干,甚至不使用InputStream和OutputStream类中已经存在的任何功能;它是一个完全独立的类,所有方法(绝大多数都只属于它自己)都是从零开始写的。这可能是因为RandomAccessFile能在文件里面前后移动,所以它的行为与其它的I/O类有些根本性的不同。总而言之,它是一个直接继承Object的,独立的类。
我们简单的认识一下RandomAccessFile的构造器:
RandomAccessFile raf=new RandomAccessFile(String path,String mode);
RandomAccessFile raf=new RandomAccessFile(File file, String mode);
第一个参数也就是路径(没有什么特别的)。
第二个参数 r 代表以只读方式打开指定文件 (只读)
rw 以读写方式打开指定文件 (读)
rws 读写方式打开,并对内容或元数据都同步写入底层存储设备
rwd 读写方式打开,对文件内容的更新同步更新至底层存储设备
他有几个特色的方法:
seek(long pos):void 设置指针到pos处,在该位置可以进行读写。
getFilePointer():long 返回单前指针所在位置
介绍了一点点该写一点点实例了(在文本任意处插入输入字段)
直接上代码:
public class Wrok_2 {
public static void main(String[] args) throws IOException {
File f=new File("D:\\Y2\\S2复习\\01 IO复习\\test\\random.txt");
insert(f, 4, "abc");
}
/**
* 任意位置插入内容
* @param f 文件对象
* @param seek 指针位置
* @param nei 插入内容
* @throws IOException
*/
static void insert(File f,long seek,String nei) throws IOException{
if(f.exists()){//是否存在
//先保存该文本中的指针所在位置之后的内容
RandomAccessFile raf=new RandomAccessFile(f, "rw");//该方法会替换之前的内容
String str=null;//保存指针后面的数据
if(f.length()==0){//如果该文本没有里面没有值,则重新赋值seek为0
seek=0;
}else{//
raf.seek(seek);//设置指针的位置
byte bs[]=new byte[1024];
while(raf.read(bs)>=0){
str+=new String(bs).trim();//trim 去空格
}
}
//开始插入内容
raf.seek(seek);//设置指针位置 为什么还要设置呢? 因为第一个seek已经失效了
raf.write(nei.getBytes());//写入用户输入的字段
//System.out.println(raf.getFilePointer());
if(str!=null){
raf.write(str.getBytes());//写入指针所在位置之后的字段
}
System.out.println("插入内容完毕!");
raf.close();//关闭
}else{//如果没有就创建
f.createNewFile();
insert(f, seek, nei);
}
}
}
没有执行上面代码之前的txt文档内容:
执行之后:
“RandomAccessFile”实现文件的多线程下载:
public static void main(String[] args) throws IOException {
/*课外:
利用“RandomAccessFile”实现文件的多线程下载*/
File f=new File("D:\\Y2\\IO复习\\01 IO复习\\test\\test.txt");
FileInputStream fis=new FileInputStream(f);
int len=(int) (f.length()/4);//获取该文件的大小并分成四分之一
byte bs[]=new byte[len];//准备4个容器
byte bs2[]=new byte[len];
byte bs3[]=new byte[len];
byte bs4[]=new byte[(int)f.length()-len*3];
fis.read(bs);//讲读到的数据保存到 byte容器中
fis.read(bs2);
fis.read(bs3);
fis.read(bs4);
/*System.out.println(new String(bs));
System.out.println(new String(bs2));
System.out.println(new String(bs3));
System.out.println(new String(bs4));*/
//System.out.println(f.length());
// System.out.println(a+" "+b+" "+c+" "+d);
new MyTheard(len*0,bs).start();//启动线程
new MyTheard(len*1,bs2).start();
new MyTheard(len*2,bs3).start();
new MyTheard(len*3,bs4).start();
}
//类中类
static class MyTheard extends Thread{
private int seek;
private byte[] bs;
MyTheard(){}
MyTheard(int seek, byte[] bs) {
this.seek = seek;
this.bs = bs;
}
public void run() {
// TODO Auto-generated method stub
RandomAccessFile raf=null;
File f=new File("D:\\Y2\\IO复习\\01 IO复习\\test\\test1.txt");
synchronized(this){
try {
raf=new RandomAccessFile(f, "rw");
raf.seek((long)this.seek);
raf.write(this.bs);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
try {
raf.close();
System.out.println("完毕");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
如有哪里错误请告诉小白(我),谢谢。