Does the following Java code guarantee an exclusive lock on an unopened file in Windows?
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File file = new File("mylog.log");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().lock();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
As may be seen in Java specs:
So if you need exclusive lock for the thread, please choose another way.