Results 1 to 4 of 4
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default File deletion won't work

    Java Code:
    boolean success = f.delete();
    System.out.println(success); //prints False

    While my java program is open, if i try to delete the file with CMD.exe, I get the error that "Cannot delete file because file is currently being used by another process". So if I close my program and run the same 'del' command it works and the file is deleted.

    My program follows this path:
    Java Code:
    1. Program Started > Load (previously saved) order files
    2. Take orders > Save/update orders to file
    3. Z report > Make sales report and clear Order List

    Everything works fine until I make a Z report which should clear the order list, it does clear the order list, but if i close and re-open the program it continues from the saved orders as if no Z was made. Therefore I had to change #3 to this:

    Java Code:
    3. Z report > Make sales report and clear Order List > Delete old order files

    I'm not so sure how to debug this, i tried to replace f.delete() with a Runtime command:
    Java Code:
    String del = "del " + f.getAbsolutePath();
    Process p2 = Runtime.getRuntime().exec(del);
    p2.waitFor();

    However that was giving me an error of its own, cannot find the file specified.
    Ex CAUSE: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    Ex MSG: Cannot run program "del": CreateProcess error=2, The system cannot find the file specified

    I do lock the files after saving them so obviously I did try to unlock the files before deleting them, with this code:
    Java Code:
        public static void deleteOrderFiles() {
            String filepath = SAVE_DIRECTORY+SAVE_DIRECTORY_ORDERS;
            java.util.List<File> fileList = searchDirectory(filepath,FILE_EXTENSION);
            boolean success;
            for (File f:fileList) {
                try {
                    String unlock = "attrib -h -r -s " + f.getAbsolutePath();
                    java.lang.Process p1 = Runtime.getRuntime().exec(unlock);
                    p1.waitFor();
                    String del = "del " + f.getAbsolutePath();
                    java.lang.Process p2 = Runtime.getRuntime().exec(del);
                    p2.waitFor();
                } catch (IOException ex) {
                    System.err.println("Ex CAUSE: " + ex.getCause());
                    System.err.println("Ex MSG: " + ex.getMessage());
                } catch (InterruptedException iex) {
                    System.err.println("IEx CAUSE: " + iex.getCause());
                    System.err.println("IEx MSG: " + iex.getMessage());
                } catch (IllegalArgumentException aex) {
                    System.err.println("AEx CAUSE: " + aex.getCause());
                    System.err.println("AEx MSG: " + aex.getMessage());
                }
            }
        }

    So does anyone know how I can delete the files successfully?

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    The simplest way you can delete is:
    Java Code:
    File f = new File("PATH");
    f.delete();
    From your error, your pathname is incorrect.
    Last edited by Solarsonic; 08-05-2011 at 08:55 AM.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Hi solarsonic, thanks for your reply. However I have a problem with that, my method:
    Java Code:
    searchDirectory(filepath,FILE_EXTENSION);
    actually returns a list of Files, and if I print out the path name, the path name is actually correct. Furthermore, the process above p2: process p1 actually successfully unlocks the Files by unhiding them and unlocking them. I have confirmed this by checking the directory myself in the MS Windows browser.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Scrap that, I just did what you said because I had a feeling that the List<File> that was being passed through the parameter wasn't referring to the actual file saved on the hard disk.

    This is my new code which works:
    Java Code:
                    String unlock = "attrib -h -r -s " + f.getAbsolutePath();
                    java.lang.Process p1 = Runtime.getRuntime().exec(unlock);
                    p1.waitFor();
                    File fileToDelete = new File(f.getAbsolutePath());
                    success = fileToDelete.delete();
                    System.out.println("File deleted: " + success);
    Thanks!

Similar Threads

  1. Incremental Deletion
    By tim in forum Forum Lobby
    Replies: 7
    Last Post: 05-18-2010, 08:39 AM
  2. deletion of files by file type
    By robby14 in forum Advanced Java
    Replies: 1
    Last Post: 05-03-2010, 08:42 AM
  3. Deletion Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:23 AM
  4. Massive Hit Deletion Help please
    By quickfingers in forum Java 2D
    Replies: 1
    Last Post: 12-24-2007, 07:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •