Java RandomAccessFile使用

       利用RandomAccessFile封装了个类:一次打开多个文件,随机访问。用于需要向大量文件随机读写,防止频繁执行打开关闭文件动作,当文件超过1min未被使用时,则自动关闭。已测试可以使用,当然该类还有可以修改完善的地方,比如读写其他类型数据,添加标志变量标识文件是否打开(此处以null判断代替)等。

package PreProcessing;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;


public class FileWriterClass {
private RandomAccessFile out;
private String path ;
private int freetime;
private int fileno;
private fileThread thread;

public FileWriterClass(String path,int fileno){
this.path = path;
this.freetime = 0;
this.out = null;
this.fileno = fileno;
this.thread = new fileThread();
}

public void openFile(){
try {
if(out==null) out = new RandomAccessFile(path,"rw");
if(!thread.isActive()) thread.start();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void closeFile(){
if(out!=null)
try {
out.close();out = null;
if(thread.isActive())  thread.stopThread();
} catch (IOException e) {
e.printStackTrace();
}
}

public synchronized void writeInt(int v){
try {
if(out!=null) {out.writeInt(v);freetime=0;}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public synchronized int readInt(){
int ret = -1;
try {
if(out!=null) {ret = out.readInt();freetime=0;}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}


public synchronized void seek(long pos){
try {
if(out!=null) {out.seek(pos);freetime=0;}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

    private class fileThread extends Thread{
    private boolean flag;
   
    public fileThread(){
    this.flag = false;
    }
   
    public void run(){
    flag = true;
    while(flag){
    freetime ++;
    if(freetime>60){
    System.out.println("文件"+fileno+"超过60s未使用,关闭!");
    closeFile();
    flag=false;
    break;//防止二次启动
   
   
    try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    flag = false;
    }
   
    public void stopThread(){
    this.flag = false;
    }
   
    public boolean isActive(){
    return flag;
    }
    }


}

//类测试

public void testClass(){

long start = new Date().getTime();
List<FileWriterClass> lst = new ArrayList<FileWriterClass>();
for(int i=0; i<375; i++){
String path = "C:\\Users\\CC\\Desktop\\Level4\\"+(i+1)+".txt";
lst.add(new FileWriterClass(path,i+1));
}

int count = 10;
while(count-->0){
for(int i=0; i<357; i++){
FileWriterClass out = lst.get(i);
out.openFile();
out.seek(1000*4);
int old = out.readInt();
old++;
out.seek(1000*4);
out.writeInt(old);
try {Thread.sleep(100);
} catch (InterruptedException e) { 
e.printStackTrace();
}
}

}

for(int i=0;i<375;i++){
lst.get(i).closeFile();
}

long end = new Date().getTime();
System.out.println("消耗时间:"+(end-start)/1000);
}

     


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值