2

I'm writing a java app that reads a File every second using FileInputStream (it's inside an infinite loop in a seperate thread, that then sleeps for 1 second)

The file is opened in another application.

When I try to save the file in the other application, I get an error message that the file is being used by another program.

I read the FileLock API, and implemented lock and release on writing and reading:

FileOutputStream fos = new FileOutputStream(file);
FileLock fileLock = fos.getChannel().lock();
if (fileLock != null) {
    fos.write(fileContent);
    fileLock.release();
}
fos.close();

How do I disable the file lock on a file that 2 applications are accessing?

Thank you!!

edit: I'm debugging on WINDOWS 7

nurnachman
  • 4,468
  • 2
  • 37
  • 40
  • 1
    Why would you want to disable the lock that your code explicitly acquired? Also, if the lock is realeased properly at the read side, this should not be a problem. You should use the proper idiom for locking, though (try-catch-finally). – Marko Topolnik Apr 12 '12 at 09:02
  • I want to disable the lock because I'm monitoring an external file that is being used by another file. – nurnachman Apr 12 '12 at 09:04
  • Is that file always locked? If yes, then check if you can write before trying to write by invoking f.canWrite(); method? – Phani Apr 12 '12 at 09:05
  • This is confusing me... you say that you **read** a file from your code and show an example of code that **writes** to a file. I assumed that means that you are in control of the code for both applications. So your real issue is that your reading code locks the file, but you would like it not to? – Marko Topolnik Apr 12 '12 at 09:09
  • @MarkoTopolnik nume is trying to write to file by acquiring a lock, so in process of that he is facing the issue. – Phani Apr 12 '12 at 09:10
  • OK, so is he writing to a file that another application also wants to write, but that other app fails? – Marko Topolnik Apr 12 '12 at 09:12
  • YES! THank you @Phani sorry for being unclear... – nurnachman Apr 12 '12 at 09:15
  • Infact this is a duplicate question find more info here: http://stackoverflow.com/questions/1500174/checking-if-a-file-locked-in-java – Phani Apr 12 '12 at 09:15
  • All right. So the problem at the Java side is not that it fails to acquire the lock, but that it fails to release it in the end? – Marko Topolnik Apr 12 '12 at 09:16
  • Yes. So, since locking is completely OS dependant. It's quite bit difficult to understand what causing the OS to not release lock. – Phani Apr 12 '12 at 09:18
  • 1
    I would still advise @nume to use the proper idiom that ensures an acquired file lock is always released. Without that, if you writing fails just once with an exception and the code that unlocks the file is not reached, that file stays locked forever, that is until reboot or some utilty unlocking it. – Marko Topolnik Apr 12 '12 at 09:25
  • @MarkoTopolnik Please write this as an answer - I will accept yours. this helped me, since I didn't close the stream. THANKS! – nurnachman Apr 12 '12 at 09:47

2 Answers2

1

That depends on your OS. I think Windows is the only OS that supports locks. Try to download Unlocker. I think there is no pure Java way for achieving this.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • Thanks for the answer! So you think there's no way to CANCEL file lock. But how do I avoid CREATING it in the first place? How do I manage my file I\O so it doesn't lock the file while another app is trying to access it? Thanks!!! – nurnachman Apr 12 '12 at 09:03
  • As far as I know, it is not possible in Windows. But I have to admit that I'm not an expert with file locks :) Maybe someone else might help. – Martijn Courteaux Apr 12 '12 at 09:07
  • Check by invoking f.canWrite(); method before trying to write. – Phani Apr 12 '12 at 09:08
  • Well, in that case please find more info from this link: http://stackoverflow.com/questions/1500174/checking-if-a-file-locked-in-java – Phani Apr 12 '12 at 09:16
1

Make sure you always release the lock you have acquired by using the proper try-finally idiom. This is the code I have in production and we have no problems with it on Windows 7:

OutputStream os = openFile();
try {
  if (os.getChannel().tryLock() == null) return;
  ... write to the file ...
}
finally { os.close(); } // this automatically releases the lock
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436