简体   繁体   中英

Not able to Close and delete already opened .xls file thro' java code

I'm trying to Delete the directory by java code, Now use case is : if file is already opened then it should close it first , and then i can directly delete the directory .

I was trying to close the file using FileoutputStream but I'm not able to make object of FileoutputStream it is showing error like "The process cannot access the file because it is being used by another process"

is there another method which will help me out ?

If the file you want to delete has been opened by your Java Application with perhaps the use of the Runtime.getRuntime().exec() method or by any other means then that file must be closed before you can carry out the task. And of course, because the file is open you can not delete the home folder (directory) of that file either, the file will need to be closed first. This is a OS safeguard which makes total sense, why would we want to delete something that is in use by something else?

In order to close the file you will need to know the name of the process which is using it, for example if a Text file named MyText.txt has been opened with MS Windows NotePad.exe then you must close the NotePad.exe process that is holding the file MyText.txt open.

Below I provide a simple method which can close an open external process for you providing you supply the proper Process Name. From our example above this would be "Notepad.exe"

public static void killProcess(String processName) {
    // PLEASE NOTE: This method will currently Kill "ALL" 
    //              open processes of the supplied process 
    //              name unless commented code changes are 
    //              made.
    try {
        Runtime rt = Runtime.getRuntime();
        // For Windows OS...
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            rt.exec("taskkill /T /F /IM " + processName);
            //rt.exec("taskkill /T /F /PID " + Long.parseLong(processName)); // Supply the PID value as string
            //rt.exec("taskkill /F /FI \"WINDOWTITLE eq " + processName + "\" /T"); // Supply the window title bar text.
            // If you want to kill only a single instance of the 
            // named process then get its PID value and use:
            // "taskkill /T /F /PID PID_Value"  OR you can provide
            // the window title and use:
            // "taskkill /F /FI \"WINDOWTITLE eq " + processName + "\" /T"
        }
        // For OSX And Linux OS's...
        else {
            rt.exec("sudo killall -9 '" + processName + "'");
            //rt.exec("kill -9 " + Long.parseLong(processName)); // Supply the PID value as string
            // If you want to kill only a single instance of the 
            // named process then get its PID value and use:
            // "kill -9 PID_Value"
        }
        rt.freeMemory();
    } 
    catch (IOException | SecurityException | IllegalArgumentException ex) {
        ex.printStackTrace();
    }
}

As the comments state at the beginning of this method code, ALL currently running processes of the supplied process name will be terminated. If you only want to terminate a specific instance of a process, as with our example above for instance, and you have four other instances of Notepad.exe open editing other text files, then you would need to change the method code to be this:

rt.exec("taskkill /F /FI \"WINDOWTITLE eq " + processName + "\" /T");

instead of this:

rt.exec("taskkill /T /F /IM " + processName);

and of course you would call this method something like this:

killProcess("MyText.txt - Notepad");

where you supply what is in the window title bar of the application. Once the above method has completed you can then delete the MyText.txt file and the folder it resides in since it is no longer being used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM