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 ;
}
質問する
3635 次
1 に答える
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);
}
}
于 2012-11-25T09:42:49.557 に答える