Unlock a file Locate the shared file you want to unlock in Windows Explorer. Right-click the file and select Unlock.
Select the file you want to unlock, or open it in the editor. Select VCS | Subversion | Unlock from the main menu, or Subversion | Unlock from the context menu of the selection.
A token representing a lock on a region of a file. A file-lock object is created each time a lock is acquired on a file via one of the lock or tryLock methods of the FileChannel class, or the lock or tryLock methods of the AsynchronousFileChannel class. A file-lock object is initially valid.
I'm having some problems deleting a file in Windows with java. For some reason, java is keeping a lock on my file, and I don't know why. Here is my code:
private byte[] getFileByteArray(File file) {
try {
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
try {
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
byte[] bt = new byte[buffer.remaining()];
buffer.get(bt);
channel.close();
raf.close();
file.delete();
return bt;
} catch (Exception ex) {
//Logger.getLogger(ConnectionImpl.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.toString());
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ConnectionImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
file.delete(), as well as trying manually in Explorer refuses to delete the file as it's still in use. All seems well in Linux though.
Am I missing a close() somehwhere? I can confirm that the method which makes the file in the first place is closing the file, as I can delete the file before running the above code using file.delete()
Extra Info: The code above is part of a method called getFileByteArray(File file) and is being called like this:
public byte[] createReport(int id) {
Report report = new Report();
String filename = report.CreateReport(id);
return getFileByteArray(new File(filename));
}
Thanks
Update: I managed to fix the issue by reading the file kilobyte by kilobyte into the byte array using ByteArrayOutputStream
As a poster below mentioned, there is a known bug in Java in that Windows has issues with file mapping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With