该进程无法访问该文件

时间:2012-11-25 09:40:54

标签: java file

I am getting following exception

    file :: Result000691.dat
    java.io.FileNotFoundException: D:\bio-rad\Result000691.dat (The process cannot access the file because it is being used by another process)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileReader.<init>(Unknown Source)
        at com.sa.medisys.biorad.io.DirectoryFetcher.read(DirectoryFetcher.java:37)
        at com.sa.medisys.biorad.io.MessageAppender.main(MessageAppender.java:28)

**When am I getting exception:**

I have folder which is stored files that I have to read ,parse ,insert into Database then need to delete.

Exception occurs while program is running and I am trying to copy new files into directory.

**My program code::** 

    while (true) {        
                DirectoryFetcher fetch = new DirectoryFetcher();
                if (fetch.hasFile()) {              
                    for(File file:fetch.getFiles()){
                        if(file.isFile()){
                        logger.info("file :: "+ file.getName());

                        long  startTime = System.nanoTime();
                        //if(file.exists()){
                            boolean flag = fetch.read(file);
                            file.delete();
                        //}                  
    //                   if(flag){
    //                      
    //                   }  
                         long  stopTime = System.nanoTime();
                         System.out.println(stopTime - startTime);
                        }                   
                    }                       
                }
            }

     boolean read(File file){
            BufferedReader br = null;
            String line = null;
            boolean flag = false;
             StringBuffer appendMessage = null;
             String completeMessage = null;
            try {
                if(file.exists()){
                    br = new BufferedReader(new FileReader(file));
                     appendMessage = new StringBuffer(16384); 
                     while ((line = br.readLine()) != null) {                
                         appendMessage.append(line);
                         appendMessage.append('\n');                 
                    }            
                     if(appendMessage != null){
                         completeMessage = appendMessage.toString();
                     }  

                   //  logger.info("Message length " + completeMessage.length());
                    // logger.info("Message::" + completeMessage);

                    if (completeMessage != null) {
                        if (!completeMessage.equals("")) {              

                            flag = new BioRad().parse(completeMessage,file);
                                // logging ACK data into file

                        }
                    }
                }   
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            }catch (Exception e) {

                e.printStackTrace();
            }

            finally{
                if(br != null){
                    try {
                        br.close();

                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }
            }


            return  flag ;
        }

1 个答案:

答案 0 :(得分:2)

我认为这个问题非常明确:

  

该进程无法访问该文件,因为该文件正由另一个进程

使用

您需要等到其他程序退出或撤消读锁定。

一种简单的处理方法是睡眠并稍后重试操作:

for(int i=0; i<MAX_TRIES; i++) {
    try {
        // your io code:
        boolean flag = fetch.read(file);
        file.delete();
        break;
    } catch(FileNotFoundException ex) {
        Thread.sleep(1000);
    }
}