How to prevent file from being overridden when reading and processing it with Java?
问题 I'd need to read and process somewhat large file with Java and I'd like to know, if there is some sensible way to protect the file that it wouldn't be overwritten by other processes while I'm reading & processing it? That is, some way to make it read-only, keep it "open" or something... This would be done in Windows environment. br, Touko 回答1: you want a FileLock: FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel(); // Try acquiring the lock without blocking. This method