By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,321 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,321 IT Pros & Developers. It's quick & easy.

Deleting Files

Nepomuk
Expert 2.5K+
P: 3,112
Hi!
I want to have my program delete some folders including all contents. For that, I wrote this method:
Expand|Select|Wrap|Line Numbers
  1. private static void delete(String source)
  2. {
  3.     File tmp = new File(source);
  4.     if(tmp.isDirectory() && tmp.list().length > 0)
  5.     {
  6.         System.out.print("** Directory: " + tmp.getName());
  7.         String[] list = tmp.list();
  8.         System.out.println(", List: " + Arrays.toString(list));
  9.         for(int i=0;i<list.length;i++)
  10.         {
  11.             //System.out.println("** Deleting " + source + File.separator + list[i]);
  12.             delete(source + File.separator + list);
  13.         }
  14.     }
  15.     if(tmp.delete())
  16.         System.out.println("** Deleted " + source);
  17.     else
  18.         System.out.println("** Not deleted " + source);
  19. }
(Source being the filesname)

As you can see, it recursively walks through all directories and deletes all Files.

Now, I get the following output when I call it with delete("E:\\examples\\"):
Expand|Select|Wrap|Line Numbers
  1. ** Directory: examples, List: [HtmlDemo.java, TicTacToe, tictactoe_source.zip]
  2. ** Deleted E:\examples\HtmlDemo.java
  3. ** Directory: TicTacToe, List: [.classpath, .project, src, src.JUnitTestsuite]
  4. ** Deleted E:\examples\TicTacToe\.classpath
  5. ** Deleted E:\examples\TicTacToe\.project
  6. ** Directory: src, List: [observerPattern, ticTacToeMVC]
  7. ** Directory: observerPattern, List: [Observable.java, Observer.java]
  8. ** Not deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
  9. ** Deleted E:\examples\TicTacToe\src\observerPattern\Observer.java
  10. ** Not deleted E:\examples\TicTacToe\src\observerPattern
  11. ** Directory: ticTacToeMVC, List: [TicTacToeApp.java, TicTacToeController.java, TicTacToeFigure.java, TicTacToeModel.java, TicTacToePlayingField.java, TicTacToeView.java]
  12. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeApp.java
  13. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeController.java
  14. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeFigure.java
  15. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeModel.java
  16. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToePlayingField.java
  17. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeView.java
  18. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC
  19. ** Not deleted E:\examples\TicTacToe\src
  20. ** Directory: src.JUnitTestsuite, List: [.classpath, .project, BlackBoxTestForView.java, WhiteBoxTestForModel.java]
  21. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.classpath
  22. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.project
  23. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\BlackBoxTestForView.java
  24. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\WhiteBoxTestForModel.java
  25. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite
  26. ** Not deleted E:\examples\TicTacToe
  27. ** Deleted E:\examples\tictactoe_source.zip
  28. ** Not deleted E:\examples
  29.  
As you can see, the file "E:\examples\TicTacToe\src\observerPattern\Observa ble.java" doesn't want to be deleted for some reason. If however I start the program (not the method - the program!) again, the output is:
Expand|Select|Wrap|Line Numbers
  1. ** Directory: examples, List: [TicTacToe]
  2. ** Directory: TicTacToe, List: [src]
  3. ** Directory: src, List: [observerPattern]
  4. ** Directory: observerPattern, List: [Observable.java]
  5. ** Deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
  6. ** Deleted E:\examples\TicTacToe\src\observerPattern
  7. ** Deleted E:\examples\TicTacToe\src
  8. ** Deleted E:\examples\TicTacToe
  9. ** Deleted E:\examples
  10.  
It works fine. The File "Observable.java" isn't opened, I should have the rights to delete it and, as you see, it works when trying the second time. However, if I try to call the method twice after each other, the output is:
Expand|Select|Wrap|Line Numbers
  1. ** Directory: examples, List: [HtmlDemo.java, TicTacToe, tictactoe_source.zip]
  2. ** Deleted E:\examples\HtmlDemo.java
  3. ** Directory: TicTacToe, List: [.classpath, .project, src, src.JUnitTestsuite]
  4. ** Deleted E:\examples\TicTacToe\.classpath
  5. ** Deleted E:\examples\TicTacToe\.project
  6. ** Directory: src, List: [observerPattern, ticTacToeMVC]
  7. ** Directory: observerPattern, List: [Observable.java, Observer.java]
  8. ** Not deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
  9. ** Deleted E:\examples\TicTacToe\src\observerPattern\Observer.java
  10. ** Not deleted E:\examples\TicTacToe\src\observerPattern
  11. ** Directory: ticTacToeMVC, List: [TicTacToeApp.java, TicTacToeController.java, TicTacToeFigure.java, TicTacToeModel.java, TicTacToePlayingField.java, TicTacToeView.java]
  12. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeApp.java
  13. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeController.java
  14. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeFigure.java
  15. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeModel.java
  16. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToePlayingField.java
  17. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeView.java
  18. ** Deleted E:\examples\TicTacToe\src\ticTacToeMVC
  19. ** Not deleted E:\examples\TicTacToe\src
  20. ** Directory: src.JUnitTestsuite, List: [.classpath, .project, BlackBoxTestForView.java, WhiteBoxTestForModel.java]
  21. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.classpath
  22. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.project
  23. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\BlackBoxTestForView.java
  24. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\WhiteBoxTestForModel.java
  25. ** Deleted E:\examples\TicTacToe\src.JUnitTestsuite
  26. ** Not deleted E:\examples\TicTacToe
  27. ** Deleted E:\examples\tictactoe_source.zip
  28. ** Not deleted E:\examples
  29. ** Directory: examples, List: [TicTacToe]
  30. ** Directory: TicTacToe, List: [src]
  31. ** Directory: src, List: [observerPattern]
  32. ** Directory: observerPattern, List: [Observable.java]
  33. ** Not deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
  34. ** Not deleted E:\examples\TicTacToe\src\observerPattern
  35. ** Not deleted E:\examples\TicTacToe\src
  36. ** Not deleted E:\examples\TicTacToe
  37. ** Not deleted E:\examples
  38.  
The output is the same, if I let it wait with
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     Thread.sleep(1000);
  4. }
  5. catch(InterruptedException ie){System.out.println("Couldn't wait");}
  6.  
Does anyone have an idea, what the problem could be?

Greetings,
Nepomuk
Sep 13 '07 #1
Share this Question
Share on Google+
12 Replies


dmjpro
100+
P: 2,476
Good Logic.
Try this code it will work.

Expand|Select|Wrap|Line Numbers
  1.       private static void delete(String source)
  2.       {
  3.           File tmp = new File(source);
  4.           if(tmp.isDirectory() && tmp.list().length > 0)
  5.           {
  6.               System.out.print("** Directory: " + tmp.getName());
  7.               String[] list = tmp.list();
  8.               System.out.println(", List: " + Arrays.toString(list));
  9.               for(int i=0;i<list.length;i++)
  10.               {
  11.                   //System.out.println("** Deleting " + source + File.separator + list);
  12.                   delete(source + File.separator + list[i]);
  13.               }
  14.           }
  15.           else{
  16.                     if(tmp.delete())
  17.                           System.out.println("** Deleted " + source);
  18.                    else
  19.                          System.out.println("** Not deleted " + source);
  20.          }
  21.       }
  22.  
Good Luck.

Kind regards,
Dmjpro.
Sep 13 '07 #2

Expert 10K+
P: 11,448
Good Logic.
Try this code it will work.

Expand|Select|Wrap|Line Numbers
  1.       private static void delete(String source)
  2.       {
  3.           File tmp = new File(source);
  4.           if(tmp.isDirectory() && tmp.list().length > 0)
  5.           {
  6.               System.out.print("** Directory: " + tmp.getName());
  7.               String[] list = tmp.list();
  8.               System.out.println(", List: " + Arrays.toString(list));
  9.               for(int i=0;i<list.length;i++)
  10.               {
  11.                   //System.out.println("** Deleting " + source + File.separator + list);
  12.                   delete(source + File.separator + list[i]);
  13.               }
  14.           }
  15.           else{
  16.                     if(tmp.delete())
  17.                           System.out.println("** Deleted " + source);
  18.                    else
  19.                          System.out.println("** Not deleted " + source);
  20.          }
  21.       }
  22.  
Good Luck.

Kind regards,
Dmjpro.
So if a File represents a directory which isn't empty it will not be deleted?
Fine logic ...

kind regards,

Jos
Sep 13 '07 #3

Nepomuk
Expert 2.5K+
P: 3,112
Good Logic.
Try this code it will work.

Expand|Select|Wrap|Line Numbers
  1.       private static void delete(String source)
  2.       {
  3.           File tmp = new File(source);
  4.           if(tmp.isDirectory() && tmp.list().length > 0)
  5.           {
  6.               System.out.print("** Directory: " + tmp.getName());
  7.               String[] list = tmp.list();
  8.               System.out.println(", List: " + Arrays.toString(list));
  9.               for(int i=0;i<list.length;i++)
  10.               {
  11.                   //System.out.println("** Deleting " + source + File.separator + list);
  12.                   delete(source + File.separator + list[i]);
  13.               }
  14.           }
  15.           else{
  16.                     if(tmp.delete())
  17.                           System.out.println("** Deleted " + source);
  18.                    else
  19.                          System.out.println("** Not deleted " + source);
  20.          }
  21.       }
  22.  
Good Luck.

Kind regards,
Dmjpro.
Thanks for trying, but evidentially the result is the same. According to the printout, and as JosAH remarked, it now doesn't even try to delete some of files!

Any other ideas?

Greetings,
Nepomuk
Sep 13 '07 #4

madhoriya22
100+
P: 252
Thanks for trying, but evidentially the result is the same. According to the printout, and as JosAH remarked, it now doesn't even try to delete some of files!

Any other ideas?

Greetings,
Nepomuk
Hi,
What is the difference in ur code and dmjpro's code ?
Sep 13 '07 #5

Nepomuk
Expert 2.5K+
P: 3,112
Hi,
What is the difference in ur code and dmjpro's code ?
It took me a while to figure that out - in line 15 he starts an else-block, which I don't have. That's all the difference.

Greetings,
Nepomuk
Sep 13 '07 #6

Expert 10K+
P: 11,448
It took me a while to figure that out - in line 15 he starts an else-block, which I don't have. That's all the difference.

Greetings,
Nepomuk
But an ever so important difference. Basically the flow runs as follows:

Expand|Select|Wrap|Line Numbers
  1. if the current thing happens to be a non-empty directory:
  2.     delete everything in it recursively.
  3. delete the thing itself.
  4.  
putting an 'else' before the last line completely changes the control flow.

@OP: I'm a bit surprised that your algorithm fails on some files/directories and
I don't have a solution for now, i.e. it sounds sensible as it is.

kind regards,

Jos
Sep 13 '07 #7

dmjpro
100+
P: 2,476
It took me a while to figure that out - in line 15 he starts an else-block, which I don't have. That's all the difference.

Greetings,
Nepomuk
Did you look at your O/P.
Your Code tries to delete Directory also.
That's why I put a else part.

Expand|Select|Wrap|Line Numbers
  1. Not deleted E:\examples\TicTacToe\src\observerPattern
  2.  
Or there may be some Outputs.........!
Look at your Output.
The recursive call is not so easy as it seems.
The flow of control is very sensible.

Kind regards,
Dmjpro.
Sep 14 '07 #8

dmjpro
100+
P: 2,476
Did you look at your O/P.
Your Code tries to delete Directory also.
That's why I put a else part.

Expand|Select|Wrap|Line Numbers
  1. Not deleted E:\examples\TicTacToe\src\observerPattern
  2.  
Or there may be some Outputs.........!
Look at your Output.
The recursive call is not so easy as it seems.
The flow of control is very sensible.

Kind regards,
Dmjpro.
Sorry nepomuk!
Your logic is right.
No need to put a else part.
Your directory gets deleted when it is empty.
And this your code says............And it ll get called recursively by LIFO method of Stack.
So sorry! for misdirecting you.
I think your total problem starts with Observable.java.
Try it with another Directory tree, see what happens.
Is Observable.java can be deleted in a normal way after shutting down your System.

Kind regards,
Dmjpro.
Sep 14 '07 #9

Nepomuk
Expert 2.5K+
P: 3,112
Sorry nepomuk!
Your logic is right.
No need to put a else part.
Your directory gets deleted when it is empty.
And this your code says............And it ll get called recursively by LIFO method of Stack.
So sorry! for misdirecting you.
I think your total problem starts with Observable.java.
Try it with another Directory tree, see what happens.
Is Observable.java can be deleted in a normal way after shutting down your System.

Kind regards,
Dmjpro.
No problem, everyone makes mistakes. ^^
I'm not so sure that the problem is with Observable.java - true, it's the file that cannot be deleted and therefore causes the others not to be deleted, but if I just start the whole program again when Observable.java and the directory it's in are the only files left, it works without a problem.

I'll check it with another directory right now and I'll post the result. Very weird stuff...

Greetings,
Nepomuk
Sep 14 '07 #10

Nepomuk
Expert 2.5K+
P: 3,112
There, different directory, same problem. Here's the output of delete("E:\\junit4.4\\"):
Expand|Select|Wrap|Line Numbers
  1. ** Directory: junit4.4, List: [cpl-v10.html, doc, javadoc, junit, junit-4.4-src.jar, junit-4.4.jar, org, README.html, temp.hamcrest.source]
  2. ** Deleted E:\junit4.4\cpl-v10.html
  3. ** Directory: doc, List: [cookbook, cookstour, faq, index.htm, ReleaseNotes4.4.html, ReleaseNotes4.4.txt, testinfected]
  4. ** Directory: cookbook, List: [cookbook.htm, logo.gif]
  5. ** Deleted E:\junit4.4\doc\cookbook\cookbook.htm
  6. ** Deleted E:\junit4.4\doc\cookbook\logo.gif
  7. ** Deleted E:\junit4.4\doc\cookbook
  8. ** Directory: cookstour, List: [cookstour.htm, Image1.gif, Image2.gif, Image3.gif, Image4.gif, Image5.gif, Image6.gif, Image7.gif]
  9. ** Not deleted E:\junit4.4\doc\cookstour\cookstour.htm
  10. ** Deleted E:\junit4.4\doc\cookstour\Image1.gif
  11. ** Deleted E:\junit4.4\doc\cookstour\Image2.gif
  12. ** Deleted E:\junit4.4\doc\cookstour\Image3.gif
  13. ** Deleted E:\junit4.4\doc\cookstour\Image4.gif
  14. ** Deleted E:\junit4.4\doc\cookstour\Image5.gif
  15. ** Deleted E:\junit4.4\doc\cookstour\Image6.gif
  16. ** Deleted E:\junit4.4\doc\cookstour\Image7.gif
  17. ** Not deleted E:\junit4.4\doc\cookstour
  18. ** Directory: faq, List: [faq.htm]
  19. ** Deleted E:\junit4.4\doc\faq\faq.htm
  20. ** Deleted E:\junit4.4\doc\faq
  21. ** Deleted E:\junit4.4\doc\index.htm
  22. ** Deleted E:\junit4.4\doc\ReleaseNotes4.4.html
  23. ** Deleted E:\junit4.4\doc\ReleaseNotes4.4.txt
  24. ** Directory: testinfected, List: [IMG00001.GIF, IMG00002.GIF, IMG00003.GIF, logo.gif, testing.htm]
  25. ** Deleted E:\junit4.4\doc\testinfected\IMG00001.GIF
  26. ** Deleted E:\junit4.4\doc\testinfected\IMG00002.GIF
  27. ** Deleted E:\junit4.4\doc\testinfected\IMG00003.GIF
  28. ** Deleted E:\junit4.4\doc\testinfected\logo.gif
  29. ** Deleted E:\junit4.4\doc\testinfected\testing.htm
  30. ** Deleted E:\junit4.4\doc\testinfected
  31. ** Not deleted E:\junit4.4\doc
  32. ** Directory: javadoc, List: [allclasses-frame.html, allclasses-noframe.html, constant-values.html, deprecated-list.html, help-doc.html, index-all.html, index.html, org, overview-frame.html, overview-summary.html, overview-tree.html, package-list, resources, serialized-form.html, stylesheet.css]
  33. ** Deleted E:\junit4.4\javadoc\allclasses-frame.html
  34. ** Deleted E:\junit4.4\javadoc\allclasses-noframe.html
  35. ** Deleted E:\junit4.4\javadoc\constant-values.html
  36. ** Deleted E:\junit4.4\javadoc\deprecated-list.html
  37. ** Deleted E:\junit4.4\javadoc\help-doc.html
  38. ** Deleted E:\junit4.4\javadoc\index-all.html
  39. ** Deleted E:\junit4.4\javadoc\index.html
  40. ** Directory: org, List: [hamcrest, junit]
  41. ** Directory: hamcrest, List: [core]
  42. ** Directory: core, List: [AllOf.html, AnyOf.html, DescribedAs.html, Is.html, IsAnything.html, IsEqual.html, IsInstanceOf.html, IsNot.html, IsNull.html, IsSame.html, package-frame.html, package-summary.html, package-tree.html]
  43. [...]
  44. ** Deleted E:\junit4.4\javadoc\org\hamcrest\core
  45. ** Deleted E:\junit4.4\javadoc\org\hamcrest
  46. ** Directory: junit, List: [After.html, AfterClass.html, Assert.html, Assume.AssumptionViolatedException.html, Assume.html, Before.html, BeforeClass.html, ComparisonFailure.html, Ignore.html, package-frame.html, package-summary.html, package-tree.html, runner, runners, Test.html, Test.None.html]
  47. [...]
  48. ** Deleted E:\junit4.4\javadoc\org\junit
  49. ** Deleted E:\junit4.4\javadoc\org
  50. ** Deleted E:\junit4.4\javadoc\overview-frame.html
  51. ** Deleted E:\junit4.4\javadoc\overview-summary.html
  52. ** Deleted E:\junit4.4\javadoc\overview-tree.html
  53. ** Deleted E:\junit4.4\javadoc\package-list
  54. ** Directory: resources, List: [inherit.gif]
  55. ** Deleted E:\junit4.4\javadoc\resources\inherit.gif
  56. ** Deleted E:\junit4.4\javadoc\resources
  57. ** Deleted E:\junit4.4\javadoc\serialized-form.html
  58. ** Deleted E:\junit4.4\javadoc\stylesheet.css
  59. ** Deleted E:\junit4.4\javadoc
  60. ** Directory: junit, List: [samples, tests]
  61. ** Directory: samples, List: [AllTests.class, AllTests.java, ListTest.class, ListTest.java, money, package-info.java, SimpleTest.class, SimpleTest.java]
  62. [...]
  63. ** Deleted E:\junit4.4\junit\samples
  64. ** Directory: tests, List: [AllTests.class, AllTests.java, extensions, framework, package-info.java, runner, WasRun.class, WasRun.java]
  65. ** Deleted E:\junit4.4\junit\tests\AllTests.class
  66. ** Deleted E:\junit4.4\junit\tests\AllTests.java
  67. [...]
  68. ** Deleted E:\junit4.4\junit\tests
  69. ** Deleted E:\junit4.4\junit
  70. ** Deleted E:\junit4.4\junit-4.4-src.jar
  71. ** Deleted E:\junit4.4\junit-4.4.jar
  72. ** Directory: org, List: [junit]
  73. ** Directory: junit, List: [samples, tests]
  74. [...]
  75. ** Deleted E:\junit4.4\org\junit
  76. ** Deleted E:\junit4.4\org
  77. ** Deleted E:\junit4.4\README.html
  78. ** Directory: temp.hamcrest.source, List: [LICENSE.txt, META-INF, org]
  79. ** Deleted E:\junit4.4\temp.hamcrest.source\LICENSE.txt
  80. ** Directory: META-INF, List: [MANIFEST.MF]
  81. ** Deleted E:\junit4.4\temp.hamcrest.source\META-INF\MANIFEST.MF
  82. ** Deleted E:\junit4.4\temp.hamcrest.source\META-INF
  83. ** Directory: org, List: [hamcrest]
  84. ** Directory: hamcrest, List: [BaseDescription.class, BaseDescription.java, BaseMatcher.class, BaseMatcher.java, core, CoreMatchers.class, CoreMatchers.java, Description.class, Description.java, Factory.class, Factory.java, internal, Matcher.class, Matcher.java, package.html, SelfDescribing.class, SelfDescribing.java, StringDescription.class, StringDescription.java]
  85. [...]
  86. ** Not deleted E:\junit4.4
  87.  
(I left out a bit of info because of the CODE-Tag-Bug.)
And when starting it a second time without changing the file system:
Expand|Select|Wrap|Line Numbers
  1. ** Directory: junit4.4, List: [doc]
  2. ** Directory: doc, List: [cookstour]
  3. ** Directory: cookstour, List: [cookstour.htm]
  4. ** Deleted E:\junit4.4\doc\cookstour\cookstour.htm
  5. ** Deleted E:\junit4.4\doc\cookstour
  6. ** Deleted E:\junit4.4\doc
  7. ** Deleted E:\junit4.4
  8.  
Just to make sure, I tried it with another directory (E:\JavaBooks) and again:
Expand|Select|Wrap|Line Numbers
  1. ** Directory: JavaBooks, List: [Handbuch der Java Programmierung, Java ist auch eine Insel]
  2. ** Directory: Handbuch der Java Programmierung, List: [hjp4exam.zip, hjp4html.zip]
  3. ** Deleted E:\JavaBooks\Handbuch der Java Programmierung\hjp4exam.zip
  4. ** Deleted E:\JavaBooks\Handbuch der Java Programmierung\hjp4html.zip
  5. ** Deleted E:\JavaBooks\Handbuch der Java Programmierung
  6. ** Directory: Java ist auch eine Insel, List: [galileocomputing_javainsel6.zip]
  7. ** Not deleted E:\JavaBooks\Java ist auch eine Insel\galileocomputing_javainsel6.zip
  8. ** Not deleted E:\JavaBooks\Java ist auch eine Insel
  9. ** Not deleted E:\JavaBooks
  10.  
But here, the second run gives me:
Expand|Select|Wrap|Line Numbers
  1. ** Directory: JavaBooks, List: [Java ist auch eine Insel]
  2. ** Directory: Java ist auch eine Insel, List: [galileocomputing_javainsel6.zip]
  3. ** Not deleted E:\JavaBooks\Java ist auch eine Insel\galileocomputing_javainsel6.zip
  4. ** Not deleted E:\JavaBooks\Java ist auch eine Insel
  5. ** Not deleted E:\JavaBooks
  6.  
I checked, but I should be allowed to delete "galileocomputing_javainsel6.zip" - and I'm able to do so manually.

Just out of curiosity, I tried to delete that exact file with delete("E:\\JavaBooks\\Java ist auch eine Insel\\galileocomputing_javainsel6.zip") and the output was:
Expand|Select|Wrap|Line Numbers
  1. ** Not deleted "E:\JavaBooks\Java ist auch eine Insel\galileocomputing_javainsel6.zip"
  2.  
What could be wrong with those files? Why can't I delete them? I don't understand...

Greetings,
Nepomuk
Sep 14 '07 #11

Expert 10K+
P: 11,448
What could be wrong with those files? Why can't I delete them? I don't understand...

Greetings,
Nepomuk
Very strange indeed; the File class has a long history of bugs; would it help
if you invoke System.gc() just before you delete the file entry? I know it sounds
silly (and it is) but give it a try and let us know the results.

kind regards,

Jos
Sep 14 '07 #12

Nepomuk
Expert 2.5K+
P: 3,112
Very strange indeed; the File class has a long history of bugs; would it help
if you invoke System.gc() just before you delete the file entry? I know it sounds
silly (and it is) but give it a try and let us know the results.

kind regards,

Jos
Thanks - it's strange, but with System.gc() I can make it work - but only if I wait as well. Here's the code I'm using:
Expand|Select|Wrap|Line Numbers
  1. private static void delete(String source)
  2. {
  3.     File tmp = new File(source);
  4.     if(tmp.isDirectory() && tmp.list().length > 0)
  5.     {
  6.         System.out.print("** Directory: " + tmp.getName());
  7.         String[] list = tmp.list();
  8.         System.out.println(", List: " + Arrays.toString(list));
  9.         for(int i=0;i<list.length;i++)
  10.         {
  11.             delete(source + File.separator + list[i]);
  12.         }
  13.     }
  14.     if(tmp.delete())
  15.         System.out.println("** Deleted " + source);
  16.     else
  17.         System.out.println("** Not deleted " + source);
  18. }
  19.  
and to call the method:
Expand|Select|Wrap|Line Numbers
  1. for(int j=0;j<2;j++)
  2. {
  3.     for(int i=0;i<source.length;i++)
  4.     {
  5.         System.gc();
  6.         delete(source[i]);
  7.     }
  8.     if(j == 0)
  9.     {
  10.         try
  11.         {
  12.             System.out.println("Taking a brake...");
  13.             Thread.sleep(3000);
  14.         }
  15.         catch(InterruptedException ie){System.out.println("Couldn't wait");}
  16.     }
  17. }
  18.  
I have no idea, why I have to wait (and it won't work with 2,5 seconds, but with 3 seconds it will... any idea, why?), but at least it work's now. I guess, I'll check, if any files still exist and if so, wait and repeat... well, for a few times, not for ever.

Thanks for your help everybody!

Greetings,
Nepomuk
Sep 14 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.