java - check if a file is already open before trying to ...
stackoverflow.com › questions › 8634377Dec 26, 2011 · public boolean isFileOpened(File file) boolean res = false; FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); // Get an exclusive lock on the whole file FileLock lock = channel.lock(); try { //The file is not already opened lock = channel.tryLock(); } catch (OverlappingFileLockException e) { // File is open by someone else res = true; } finally { lock.release(); } return res; }
java - Check if file is already open - Stack Overflow
stackoverflow.com › questions › 1390592Sep 07, 2009 · // TO CHECK WHETHER A FILE IS OPENED // OR NOT (not for .txt files) // the file we want to check String fileName = "C:\\Text.xlsx"; File file = new File(fileName); // try to rename the file with the same name File sameFileName = new File(fileName); if(file.renameTo(sameFileName)){ // if the file is renamed System.out.println("file is closed"); }else{ // if the file didnt accept the renaming operation System.out.println("file is opened"); }
How to know whether a particular file is still opened or ...
stackoverflow.com › questions › 13577466Nov 27, 2012 · Show activity on this post. I have a scenario where i have used to open a file using the java commands in following steps. a) creating File object for the location of file. File f = new File (filePath); b) then using system application (like Excel) to open the file. Desktop desktop = null; if (Desktop.isDesktopSupported ()) { desktop = Desktop.getDesktop (); } try { desktop.open (f); } catch (IOException e) { }