Expert 2GB |
Hi!
I want to have my program delete some folders including all contents. For that, I wrote this method: -
private static void delete(String source)
-
{
-
File tmp = new File(source);
-
if(tmp.isDirectory() && tmp.list().length > 0)
-
{
-
System.out.print("** Directory: " + tmp.getName());
-
String[] list = tmp.list();
-
System.out.println(", List: " + Arrays.toString(list));
-
for(int i=0;i<list.length;i++)
-
{
-
//System.out.println("** Deleting " + source + File.separator + list[i]);
-
delete(source + File.separator + list);
-
}
-
}
-
if(tmp.delete())
-
System.out.println("** Deleted " + source);
-
else
-
System.out.println("** Not deleted " + source);
-
}
-
(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\\"): -
** Directory: examples, List: [HtmlDemo.java, TicTacToe, tictactoe_source.zip]
-
** Deleted E:\examples\HtmlDemo.java
-
** Directory: TicTacToe, List: [.classpath, .project, src, src.JUnitTestsuite]
-
** Deleted E:\examples\TicTacToe\.classpath
-
** Deleted E:\examples\TicTacToe\.project
-
** Directory: src, List: [observerPattern, ticTacToeMVC]
-
** Directory: observerPattern, List: [Observable.java, Observer.java]
-
** Not deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
-
** Deleted E:\examples\TicTacToe\src\observerPattern\Observer.java
-
** Not deleted E:\examples\TicTacToe\src\observerPattern
-
** Directory: ticTacToeMVC, List: [TicTacToeApp.java, TicTacToeController.java, TicTacToeFigure.java, TicTacToeModel.java, TicTacToePlayingField.java, TicTacToeView.java]
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeApp.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeController.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeFigure.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeModel.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToePlayingField.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeView.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC
-
** Not deleted E:\examples\TicTacToe\src
-
** Directory: src.JUnitTestsuite, List: [.classpath, .project, BlackBoxTestForView.java, WhiteBoxTestForModel.java]
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.classpath
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.project
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\BlackBoxTestForView.java
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\WhiteBoxTestForModel.java
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite
-
** Not deleted E:\examples\TicTacToe
-
** Deleted E:\examples\tictactoe_source.zip
-
** Not deleted E:\examples
-
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: -
** Directory: examples, List: [TicTacToe]
-
** Directory: TicTacToe, List: [src]
-
** Directory: src, List: [observerPattern]
-
** Directory: observerPattern, List: [Observable.java]
-
** Deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
-
** Deleted E:\examples\TicTacToe\src\observerPattern
-
** Deleted E:\examples\TicTacToe\src
-
** Deleted E:\examples\TicTacToe
-
** Deleted E:\examples
-
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: -
** Directory: examples, List: [HtmlDemo.java, TicTacToe, tictactoe_source.zip]
-
** Deleted E:\examples\HtmlDemo.java
-
** Directory: TicTacToe, List: [.classpath, .project, src, src.JUnitTestsuite]
-
** Deleted E:\examples\TicTacToe\.classpath
-
** Deleted E:\examples\TicTacToe\.project
-
** Directory: src, List: [observerPattern, ticTacToeMVC]
-
** Directory: observerPattern, List: [Observable.java, Observer.java]
-
** Not deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
-
** Deleted E:\examples\TicTacToe\src\observerPattern\Observer.java
-
** Not deleted E:\examples\TicTacToe\src\observerPattern
-
** Directory: ticTacToeMVC, List: [TicTacToeApp.java, TicTacToeController.java, TicTacToeFigure.java, TicTacToeModel.java, TicTacToePlayingField.java, TicTacToeView.java]
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeApp.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeController.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeFigure.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeModel.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToePlayingField.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC\TicTacToeView.java
-
** Deleted E:\examples\TicTacToe\src\ticTacToeMVC
-
** Not deleted E:\examples\TicTacToe\src
-
** Directory: src.JUnitTestsuite, List: [.classpath, .project, BlackBoxTestForView.java, WhiteBoxTestForModel.java]
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.classpath
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\.project
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\BlackBoxTestForView.java
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite\WhiteBoxTestForModel.java
-
** Deleted E:\examples\TicTacToe\src.JUnitTestsuite
-
** Not deleted E:\examples\TicTacToe
-
** Deleted E:\examples\tictactoe_source.zip
-
** Not deleted E:\examples
-
** Directory: examples, List: [TicTacToe]
-
** Directory: TicTacToe, List: [src]
-
** Directory: src, List: [observerPattern]
-
** Directory: observerPattern, List: [Observable.java]
-
** Not deleted E:\examples\TicTacToe\src\observerPattern\Observable.java
-
** Not deleted E:\examples\TicTacToe\src\observerPattern
-
** Not deleted E:\examples\TicTacToe\src
-
** Not deleted E:\examples\TicTacToe
-
** Not deleted E:\examples
-
The output is the same, if I let it wait with -
try
-
{
-
Thread.sleep(1000);
-
}
-
catch(InterruptedException ie){System.out.println("Couldn't wait");}
-
Does anyone have an idea, what the problem could be?
Greetings,
Nepomuk
| |
Share:
2GB |
Good Logic.
Try this code it will work. -
private static void delete(String source)
-
{
-
File tmp = new File(source);
-
if(tmp.isDirectory() && tmp.list().length > 0)
-
{
-
System.out.print("** Directory: " + tmp.getName());
-
String[] list = tmp.list();
-
System.out.println(", List: " + Arrays.toString(list));
-
for(int i=0;i<list.length;i++)
-
{
-
//System.out.println("** Deleting " + source + File.separator + list);
-
delete(source + File.separator + list[i]);
-
}
-
}
-
else{
-
if(tmp.delete())
-
System.out.println("** Deleted " + source);
-
else
-
System.out.println("** Not deleted " + source);
-
}
-
}
-
Good Luck.
Kind regards,
Dmjpro.
| | Expert 8TB |
Good Logic.
Try this code it will work. -
private static void delete(String source)
-
{
-
File tmp = new File(source);
-
if(tmp.isDirectory() && tmp.list().length > 0)
-
{
-
System.out.print("** Directory: " + tmp.getName());
-
String[] list = tmp.list();
-
System.out.println(", List: " + Arrays.toString(list));
-
for(int i=0;i<list.length;i++)
-
{
-
//System.out.println("** Deleting " + source + File.separator + list);
-
delete(source + File.separator + list[i]);
-
}
-
}
-
else{
-
if(tmp.delete())
-
System.out.println("** Deleted " + source);
-
else
-
System.out.println("** Not deleted " + source);
-
}
-
}
-
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
| | Expert 2GB |
Good Logic.
Try this code it will work. -
private static void delete(String source)
-
{
-
File tmp = new File(source);
-
if(tmp.isDirectory() && tmp.list().length > 0)
-
{
-
System.out.print("** Directory: " + tmp.getName());
-
String[] list = tmp.list();
-
System.out.println(", List: " + Arrays.toString(list));
-
for(int i=0;i<list.length;i++)
-
{
-
//System.out.println("** Deleting " + source + File.separator + list);
-
delete(source + File.separator + list[i]);
-
}
-
}
-
else{
-
if(tmp.delete())
-
System.out.println("** Deleted " + source);
-
else
-
System.out.println("** Not deleted " + source);
-
}
-
}
-
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
| | 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 ?
| | 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
| | 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: -
if the current thing happens to be a non-empty directory:
-
delete everything in it recursively.
-
delete the thing itself.
-
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
| | 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. -
Not deleted E:\examples\TicTacToe\src\observerPattern
-
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.
| | 2GB |
Did you look at your O/P.
Your Code tries to delete Directory also.
That's why I put a else part. -
Not deleted E:\examples\TicTacToe\src\observerPattern
-
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.
| | 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
| | Expert 2GB |
There, different directory, same problem. Here's the output of delete("E:\\junit4.4\\"): -
** 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]
-
** Deleted E:\junit4.4\cpl-v10.html
-
** Directory: doc, List: [cookbook, cookstour, faq, index.htm, ReleaseNotes4.4.html, ReleaseNotes4.4.txt, testinfected]
-
** Directory: cookbook, List: [cookbook.htm, logo.gif]
-
** Deleted E:\junit4.4\doc\cookbook\cookbook.htm
-
** Deleted E:\junit4.4\doc\cookbook\logo.gif
-
** Deleted E:\junit4.4\doc\cookbook
-
** Directory: cookstour, List: [cookstour.htm, Image1.gif, Image2.gif, Image3.gif, Image4.gif, Image5.gif, Image6.gif, Image7.gif]
-
** Not deleted E:\junit4.4\doc\cookstour\cookstour.htm
-
** Deleted E:\junit4.4\doc\cookstour\Image1.gif
-
** Deleted E:\junit4.4\doc\cookstour\Image2.gif
-
** Deleted E:\junit4.4\doc\cookstour\Image3.gif
-
** Deleted E:\junit4.4\doc\cookstour\Image4.gif
-
** Deleted E:\junit4.4\doc\cookstour\Image5.gif
-
** Deleted E:\junit4.4\doc\cookstour\Image6.gif
-
** Deleted E:\junit4.4\doc\cookstour\Image7.gif
-
** Not deleted E:\junit4.4\doc\cookstour
-
** Directory: faq, List: [faq.htm]
-
** Deleted E:\junit4.4\doc\faq\faq.htm
-
** Deleted E:\junit4.4\doc\faq
-
** Deleted E:\junit4.4\doc\index.htm
-
** Deleted E:\junit4.4\doc\ReleaseNotes4.4.html
-
** Deleted E:\junit4.4\doc\ReleaseNotes4.4.txt
-
** Directory: testinfected, List: [IMG00001.GIF, IMG00002.GIF, IMG00003.GIF, logo.gif, testing.htm]
-
** Deleted E:\junit4.4\doc\testinfected\IMG00001.GIF
-
** Deleted E:\junit4.4\doc\testinfected\IMG00002.GIF
-
** Deleted E:\junit4.4\doc\testinfected\IMG00003.GIF
-
** Deleted E:\junit4.4\doc\testinfected\logo.gif
-
** Deleted E:\junit4.4\doc\testinfected\testing.htm
-
** Deleted E:\junit4.4\doc\testinfected
-
** Not deleted E:\junit4.4\doc
-
** 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]
-
** Deleted E:\junit4.4\javadoc\allclasses-frame.html
-
** Deleted E:\junit4.4\javadoc\allclasses-noframe.html
-
** Deleted E:\junit4.4\javadoc\constant-values.html
-
** Deleted E:\junit4.4\javadoc\deprecated-list.html
-
** Deleted E:\junit4.4\javadoc\help-doc.html
-
** Deleted E:\junit4.4\javadoc\index-all.html
-
** Deleted E:\junit4.4\javadoc\index.html
-
** Directory: org, List: [hamcrest, junit]
-
** Directory: hamcrest, List: [core]
-
** 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]
-
[...]
-
** Deleted E:\junit4.4\javadoc\org\hamcrest\core
-
** Deleted E:\junit4.4\javadoc\org\hamcrest
-
** 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]
-
[...]
-
** Deleted E:\junit4.4\javadoc\org\junit
-
** Deleted E:\junit4.4\javadoc\org
-
** Deleted E:\junit4.4\javadoc\overview-frame.html
-
** Deleted E:\junit4.4\javadoc\overview-summary.html
-
** Deleted E:\junit4.4\javadoc\overview-tree.html
-
** Deleted E:\junit4.4\javadoc\package-list
-
** Directory: resources, List: [inherit.gif]
-
** Deleted E:\junit4.4\javadoc\resources\inherit.gif
-
** Deleted E:\junit4.4\javadoc\resources
-
** Deleted E:\junit4.4\javadoc\serialized-form.html
-
** Deleted E:\junit4.4\javadoc\stylesheet.css
-
** Deleted E:\junit4.4\javadoc
-
** Directory: junit, List: [samples, tests]
-
** Directory: samples, List: [AllTests.class, AllTests.java, ListTest.class, ListTest.java, money, package-info.java, SimpleTest.class, SimpleTest.java]
-
[...]
-
** Deleted E:\junit4.4\junit\samples
-
** Directory: tests, List: [AllTests.class, AllTests.java, extensions, framework, package-info.java, runner, WasRun.class, WasRun.java]
-
** Deleted E:\junit4.4\junit\tests\AllTests.class
-
** Deleted E:\junit4.4\junit\tests\AllTests.java
-
[...]
-
** Deleted E:\junit4.4\junit\tests
-
** Deleted E:\junit4.4\junit
-
** Deleted E:\junit4.4\junit-4.4-src.jar
-
** Deleted E:\junit4.4\junit-4.4.jar
-
** Directory: org, List: [junit]
-
** Directory: junit, List: [samples, tests]
-
[...]
-
** Deleted E:\junit4.4\org\junit
-
** Deleted E:\junit4.4\org
-
** Deleted E:\junit4.4\README.html
-
** Directory: temp.hamcrest.source, List: [LICENSE.txt, META-INF, org]
-
** Deleted E:\junit4.4\temp.hamcrest.source\LICENSE.txt
-
** Directory: META-INF, List: [MANIFEST.MF]
-
** Deleted E:\junit4.4\temp.hamcrest.source\META-INF\MANIFEST.MF
-
** Deleted E:\junit4.4\temp.hamcrest.source\META-INF
-
** Directory: org, List: [hamcrest]
-
** 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]
-
[...]
-
** Not deleted E:\junit4.4
-
(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: -
** Directory: junit4.4, List: [doc]
-
** Directory: doc, List: [cookstour]
-
** Directory: cookstour, List: [cookstour.htm]
-
** Deleted E:\junit4.4\doc\cookstour\cookstour.htm
-
** Deleted E:\junit4.4\doc\cookstour
-
** Deleted E:\junit4.4\doc
-
** Deleted E:\junit4.4
-
Just to make sure, I tried it with another directory (E:\JavaBooks) and again: -
** Directory: JavaBooks, List: [Handbuch der Java Programmierung, Java ist auch eine Insel]
-
** Directory: Handbuch der Java Programmierung, List: [hjp4exam.zip, hjp4html.zip]
-
** Deleted E:\JavaBooks\Handbuch der Java Programmierung\hjp4exam.zip
-
** Deleted E:\JavaBooks\Handbuch der Java Programmierung\hjp4html.zip
-
** Deleted E:\JavaBooks\Handbuch der Java Programmierung
-
** Directory: Java ist auch eine Insel, List: [galileocomputing_javainsel6.zip]
-
** Not deleted E:\JavaBooks\Java ist auch eine Insel\galileocomputing_javainsel6.zip
-
** Not deleted E:\JavaBooks\Java ist auch eine Insel
-
** Not deleted E:\JavaBooks
-
But here, the second run gives me: -
** Directory: JavaBooks, List: [Java ist auch eine Insel]
-
** Directory: Java ist auch eine Insel, List: [galileocomputing_javainsel6.zip]
-
** Not deleted E:\JavaBooks\Java ist auch eine Insel\galileocomputing_javainsel6.zip
-
** Not deleted E:\JavaBooks\Java ist auch eine Insel
-
** Not deleted E:\JavaBooks
-
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: -
** Not deleted "E:\JavaBooks\Java ist auch eine Insel\galileocomputing_javainsel6.zip"
-
What could be wrong with those files? Why can't I delete them? I don't understand...
Greetings,
Nepomuk
| | 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
| | 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: -
private static void delete(String source)
-
{
-
File tmp = new File(source);
-
if(tmp.isDirectory() && tmp.list().length > 0)
-
{
-
System.out.print("** Directory: " + tmp.getName());
-
String[] list = tmp.list();
-
System.out.println(", List: " + Arrays.toString(list));
-
for(int i=0;i<list.length;i++)
-
{
-
delete(source + File.separator + list[i]);
-
}
-
}
-
if(tmp.delete())
-
System.out.println("** Deleted " + source);
-
else
-
System.out.println("** Not deleted " + source);
-
}
-
and to call the method: -
for(int j=0;j<2;j++)
-
{
-
for(int i=0;i<source.length;i++)
-
{
-
System.gc();
-
delete(source[i]);
-
}
-
if(j == 0)
-
{
-
try
-
{
-
System.out.println("Taking a brake...");
-
Thread.sleep(3000);
-
}
-
catch(InterruptedException ie){System.out.println("Couldn't wait");}
-
}
-
}
-
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
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
reply
views
Thread by MichaĆ Januszczyk |
last post: by
|
5 posts
views
Thread by Rosa |
last post: by
|
3 posts
views
Thread by Mike Turco |
last post: by
|
reply
views
Thread by Hrvoje Vrbanc |
last post: by
|
6 posts
views
Thread by Martin Bischoff |
last post: by
|
3 posts
views
Thread by Martin Ho |
last post: by
|
2 posts
views
Thread by Josh Kandiko |
last post: by
|
7 posts
views
Thread by eSolTec, Inc. 501(c)(3) |
last post: by
|
3 posts
views
Thread by Kimera.Kimera@gmail.com |
last post: by
| | | | | | | | | | | |