I have a list of files in a folder, I want to lock a specific file(user send me the name of the file to be locked), which i am doing as below:
try {
File file = new File("filename");
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
FileLock lock = channel.lock();
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
// File is already locked
}
} catch (Exception e) {
}
And if another user wants to see the list of files i have to tell them the status of file which is locked and which is unlocked
File folder = new File("E:\\folder_to_LIST_OF_FILES");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
FilesDto returnDto = new FilesDto();
returnDto.setFileName(FilenameUtils.removeExtension(listOfFiles[i].getName()));
// Check File Status if file is Locked or unlocked
if (lock==null) {
returnDto.setStatus("unlocked");
returnDto.setFilePath(listOfFiles[i].getAbsolutePath());
} else {
returnDto.setStatus("Locked");
}
returnDtoList.add(returnDto);
}
}
These two snippets are from different API's. How To Check the status of File it is locked or unlocked?