File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deleting the Files from Directory

 
prashantpk kulkarni
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm trying to delete the files from a directory below is code

if(src.isDirectory()){
//list all the directory contents
String files[] = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
deleteFile(srcFile);

}

}

public static boolean deleteFile(File srcFile)
{
try{

Boolean status=srcFile.delete();

}
catch (Exception e) {
return false;
}
return true;
}

But files are not deleting from directory srcFile.delete(); should return true but in our case returns false value.
Please help me out.

Thanks advance.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prashantpk kulkarni wrote:Hi All,

I'm trying to delete the files from a directory below is code


But files are not deleting from directory srcFile.delete(); should return true but in our case returns false value.
Please help me out.

Thanks advance.


Perhaps it would be a good idea to print out the exception -- and maybe the stacktrace too -- instead of just returning false? Why ask us for the reason, when you computer is trying to tell you via the exception?

Henry
 
prashantpk kulkarni
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

When i removed return statement and added below code same thing files are not deleting

public static boolean deleteFile(File srcFile)
{
try{

Boolean status=srcFile.delete();

}
catch (Exception e) {
log.error("<----:Exception happen!---->",e.printStackTrace());
}
return true;
}
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the Stack Trace TELL you? That information is given to you for a reason - don't just ignore it.

If you want us to help you, please post EXACTLY what it says. Usually the first few lines are enough.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic