process can't access file bcos another process has locked a portion of file
843810Dec 24 2002 — edited Feb 4 2003Does anyone knows if Java could handle the well-known "Open File" problem.
I encounter the following error when I was trying to copy an outlook file to another location:
java.io.IOException: The process cannot access the file because another process has locked a portion of the file.
My codes is placed below for reference.
Note that I was only trying to open to open the file for reading the Inputstream. Please advise what I could do to avoid getting caught in exception while reading the bytes.
It will be good if Java could handle the Microsoft's "Open File" problem.
// my codes
public static void main(String[] args)
{
int BUFSIZE = 4096;
File theFile = new File("C:\\Documents and Settings\\" + myID +
"\\Local Settings\\Application Data\\" +
"Microsoft\\Outlook\\mail.pst");
long size = theFile.length();
try {
FileInputStream fis = new FileInputStream(theFile);
// exception will happen on the next line
if (!(size >= 0 &&
fis.read(new byte[BUFSIZE],0,(int)Math.min(size,BUFSIZE)) > -1)) {
System.out.println("encountered OFM problem");
} else {
System.out.println("read file successfully");
}
} catch (Exception e) {
System.out.println("caught OFM problem");
e.printStackTrace();
}
}