Results 1 to 8 of 8
  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 New File.delete() problem

    I had a problem with deleting my files before which I made another post for, this is not that problem, but if you want some background info you can read this:
    File deletion won't work

    When my program has no files to load, it creates new ones and can delete them fine.

    My problem happens when my program has files to load, the loaded files will not delete but any new files created will be deleted. So my questions are:

    1. Why does this happen/How can I fix this?
    2. Assuming the files are being locked by a process, if they are still in memory how can I get rid of them so they can be deleted?

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

    Default

    These are the methods i'm using to load files and delete files:

    Java Code:
        [COLOR="Blue"]public static[/COLOR] java.util.List<File> searchDirectory(String pathname, String filetype) {
            String filename;
            java.util.List<File> foundFiles = new java.util.ArrayList<File>();
            java.util.List<File> listOfFiles = new java.util.ArrayList<File>();
            File folder = new File(pathname);
            [COLOR="Blue"]if[/COLOR] (folder != [COLOR="blue"]null[/COLOR]) {
                listOfFiles.addAll(java.util.Arrays.asList(folder.listFiles()));
                [COLOR="blue"]for[/COLOR] (int i=0; i<listOfFiles.size(); i++) {
                    [COLOR="blue"]if[/COLOR] (listOfFiles.get(i).isFile()) {
                        filename = listOfFiles.get(i).getName();
                        [COLOR="blue"]if[/COLOR] (filename.endsWith(filetype)) {
                            foundFiles.add(listOfFiles.get(i));
                        }
                    }
                }
            }
            [COLOR="Blue"]return[/COLOR] foundFiles;
        }
    
        [COLOR="blue"]public static[/COLOR] java.util.List<Order> getOrders() {
            java.util.List<File> myFiles = [COLOR="red"]searchDirectory[/COLOR]([COLOR="red"]createDirectory[/COLOR]([COLOR="Magenta"]SAVE_DIRECTORY_ORDERS[/COLOR]),[COLOR="magenta"]FILE_EXTENSION[/COLOR]);
            java.util.List<Order> myOrders = new java.util.ArrayList<Order>();
            [COLOR="blue"]for[/COLOR] (File f:myFiles) {
                myOrders.add([COLOR="red"]readOrder[/COLOR](f.getAbsolutePath()));
            }
            [COLOR="blue"]return[/COLOR] myOrders;
        }
    
        [COLOR="blue"]public static[/COLOR] void deleteOrderFiles() {
            String filepath = [COLOR="magenta"]SAVE_DIRECTORY[/COLOR]+[COLOR="magenta"]SAVE_DIRECTORY_ORDERS[/COLOR];
            java.util.List<File> fileList = [COLOR="red"]searchDirectory[/COLOR](filepath,[COLOR="magenta"]FILE_EXTENSION[/COLOR]);
            boolean success;
            [COLOR="blue"]for[/COLOR] (File f:fileList) {
                [COLOR="blue"]try[/COLOR] {
                    String unlock = "attrib -h -r -s " + f.getAbsolutePath();
                    java.lang.Process p1 = Runtime.getRuntime().exec(unlock);
                    p1.waitFor();
                    File fileToDelete = [COLOR="blue"]new[/COLOR] File(f.getAbsolutePath());
                    success = fileToDelete.delete();
                    [COLOR="YellowGreen"]System.out.println[/COLOR]("File deleted: " + success);
                } [COLOR="blue"]catch[/COLOR] (IOException ex) {
                    ...
                } [COLOR="blue"]catch[/COLOR] (InterruptedException iex) {
                    ...
                } [COLOR="Blue"]catch[/COLOR] (IllegalArgumentException aex) {
                    ...
                }
            }
        }
    Last edited by ozzyman; 04-01-2011 at 03:53 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    A last-resort kind of approach, when all else that should work, doesn't, is to explicitly set all variables connected with the file to null (close any streams first!), call System.gc() and then try to delete the file. I've seen reports that this makes a difference.

    db

  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

    Thanks darryl. I got quite an interesting result from calling the garbage collector, the only change I made was adding System.gc(); before the file is deleted. Strangely all files were deleted except the first one. I've tried this many times, even deleting the whole folder myself and starting the program again. This is always the case: the first file order1.pos is never deleted.

    So i'm going to nullify the order Lists now to see if the first file can also be deleted. Thanks again for the help and i'll post back my results shortly =).

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

    Default

    Unfortunately, even after setting lists to null I have the same problem.

    This is the code that gets run when a Z report is made:

    Java Code:
                        ...
                        OrdersIO.writeNewOrders(askNewOrder);
                        askNewOrder.clear();
                        currentOrder = -1;
                        [COLOR="red"]askNewOrder = null;[/COLOR]
                        OrdersIO.deleteOrderFiles();
                        break;

    I nullified askNewOrder because when the files are loaded they are fed straight into askNewOrder:

    Java Code:
        public void loadOrderState() {
            askNewOrder = OrdersIO.getOrders();
            System.out.println("Loaded " + askNewOrder.size() + " orders.");
            currentOrder = askNewOrder.size()-1;
            ...
        }

    The only other list that is used when files are loaded is in the getOrders() method, so I nullified that list too but to no avail.

    Java Code:
        public static List<Order> getOrders() {
            List<File> myFiles = searchDirectory(createDirectory(SAVE_DIRECTORY_ORDERS),FILE_EXTENSION);
            List<Order> myOrders = new ArrayList<Order>();
            for (File f:myFiles) {
                myOrders.add(readOrder(f.getAbsolutePath()));
            }
            [COLOR="red"]myFiles = null;[/COLOR]
            return myOrders;
        }

    Besides this, I can't think of why only the first item in the list can't be deleted, because whenever the lists are loaded they are done so together.

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

    Default

    Okay I realised that calling the garbage collector inside the for-loop was bad practice so I removed it and placed it before the for-loop begins to start deleting files, and now All the files are deleted successfully! Cheers!

    Java Code:
    run:
    Loaded 5 orders.
    Order #6
    Order #7
    Z 1:
    [2]  4.50
    [3]  30.70
    [4]  32.95
    GT:  68.15 ( 0.00)
    VOID:  0.00
    NET:  68.15
    File deleted: true
    File deleted: true
    File deleted: true
    File deleted: true
    File deleted: true
    File deleted: true
    File deleted: true
    BUILD SUCCESSFUL (total time: 20 seconds)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Glad that got sorted out! Now keep an eye out for similar problems so you can be of help :)

    db

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

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Glad that got sorted out! Now keep an eye out for similar problems so you can be of help :)

    db
    Definately :). System.gc().

Similar Threads

  1. Can't Delete A File
    By blazecode in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-07-2011, 11:16 AM
  2. Delete A File
    By nitinverma in forum CLDC and MIDP
    Replies: 13
    Last Post: 06-15-2010, 09:12 AM
  3. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 11:17 AM
  4. problem when I try to delete a file
    By tommy in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 03:44 PM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 09:11 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
  •