• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleteing the directory.  RSS feed

 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am using another process in detail say C# code to generate/write files into the file system.

Then I am trying to delete the folder but it is not deleting the folder.

Not Deleting : When you call Delete logic from main program where I am using C# code to generate the files.

But i am able to delete as separate program. Spent many days on this.

Please tell me what is the issue.

The delete logic is :

public void deleteDirectory(File path) throws MsgParserException {

if (!path.exists()) {
System.out.println("No such file or directory: " + path);
} else {
System.out.println(path + " is existed.");
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
System.out.println(files[i]
+ " is a dir.. so calling again.");
deleteDirectory(files[i]);
} else {
files[i].delete();
System.out.println(files[i] + " is deleted.");
}
}
setDeleted(true);
path.delete();

}

}


 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I fully understand your problem.

You might have insufficient privileges to manage those files ... here a bit tweaked version of your code.

 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,

The problem here is in the logic. You are checking whether a File object is a directory or not. If its not, it deletes the file. If its a directory, it empties the directory by calling it recursively. But, the directory is not deleted.

Modified code:


Please note that, I didn't check this code.

Hope this solves the issue.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that what the ending path.delete() should do?

Praveen, I suggest you check which delete() call returns false. Replace your "x.delete()" calls with (here x is either path or files[i])
 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. The problem is the folder/files are write protected.

Now I want ro resolve this issue.

In java, can i make a write protected file to writable and i delete it.

How we will create readable and writable file in java/.Net.

Please help me.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can. Java documentation. You could have looked it up yourself ...



Keep in mind this can only be done in Java 1.6 and upward.
 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , Thanks a lot.

Still I am not able to delete the files.

I made setWritable(true) and setReadble(false) ... Still i am not able to delete.

I think files are locked for some time. IS there any way to release the lock(s) for the file ?


it is urgent

Thanks
Praveen.
 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... small spell mistake

I made setWritable(true) and setReadble(true) ... Still i am not able to delete.
 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resolved the issue.


Need to close all previous input streams in the project.

Thanks
Praveen
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that makes sense. you should always close any resources when you're done with them. The best way is to use a try-finally block, with the closing in the finally:
This way, even if the code in the try block throws an exception, the stream is still closed.
 
Carey Brown
Saloon Keeper
Posts: 3246
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in

files may end up being set to null which will throw a NPE in the line that follows. Even prechecking path to see if isDirectory() returns true is not a guarantee that files will be non-null.

If you can't delete a file after all that then I presume that the file is in use by some other process. I found that some of Windows command line utilities will delete files that Java has not been able to delete. Were they in use? Were they system files? Were they hidden files?

Carey
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, if you'd have read Praveen's last post you'd have seen that the problem was simply having open FileInputStreams to files inside the folder.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!