Results 1 to 8 of 8
Thread: New File.delete() problem
-
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?
-
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 02:53 PM.
- 04-01-2011, 02:38 PM #3
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
-
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 =).
-
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.
-
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)
- 04-01-2011, 04:15 PM #7
Glad that got sorted out! Now keep an eye out for similar problems so you can be of help :)
db
-
Similar Threads
-
Can't Delete A File
By blazecode in forum Threads and SynchronizationReplies: 4Last Post: 03-07-2011, 10:16 AM -
Delete A File
By nitinverma in forum CLDC and MIDPReplies: 13Last Post: 06-15-2010, 08:12 AM -
Delete From .txt file
By Sarinam in forum New To JavaReplies: 86Last Post: 06-28-2008, 10:17 AM -
problem when I try to delete a file
By tommy in forum Advanced JavaReplies: 2Last Post: 07-31-2007, 02:44 PM -
How to delete a file
By Alpha in forum New To JavaReplies: 1Last Post: 05-26-2007, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks