473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Deleting Files

Nepomuk
3,112 Expert 2GB
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
12 2419
dmjpro
2,476 2GB
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
JosAH
11,448 Expert 8TB
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
3,112 Expert 2GB
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
252 100+
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
3,112 Expert 2GB
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
JosAH
11,448 Expert 8TB
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
2,476 2GB
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
2,476 2GB
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
3,112 Expert 2GB
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
3,112 Expert 2GB
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
JosAH
11,448 Expert 8TB
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
3,112 Expert 2GB
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

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

Similar topics

0
by: MichaƂ Januszczyk | last post by:
Hello. I've got the following problem: I created a windows installer project that installs my application. The application works with some files. I want to include some example files with the...
5
by: Rosa | last post by:
Hi, I'm trying to clear the TIF on Windows XP programmatically with the below code. This code works fine on any folder but the TIF. For some reason the atEnd() statements always defaults to true...
3
by: Mike Turco | last post by:
I'm working on an application that imports and exports tons of CSV data, like 64,000 records per file, and six or seven files in a set. First off, by the tine I import a few hundred thousand...
0
by: Hrvoje Vrbanc | last post by:
Hello, this is a problem I came upon while building a site based on MCMS 2002 but it's not strictly MCMS-oriented: I have a page that displays a certain content in presentation mode but when an...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
3
by: Martin Ho | last post by:
Can someone help me with this please? I wasn't very clear in my old post. I have a program to copy files from one location to another, now I want to copy only those files which were created on...
2
by: Josh Kandiko | last post by:
Hi, I've been having some frustrations with deleting files from the Isolated Storage directories. Basically, I want my application to remove all instances of configuration information for my...
7
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I have an application that pulls files, folders and registry keys of installed programs. I'm wanting to with a context menu selection of "Delete...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
0
by: DanInTacoma | last post by:
We have a fairly straightforward maint plan on one of our servers (MSSQL 2K)...it backs up several DB's to a folder on another server (the one with the tape drive) so they can be archived to tape. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.