wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Deleting the Files from Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Deleting the Files from Directory" Watch "Deleting the Files from Directory" New topic
Author

Deleting the Files from Directory

prashantpk kulkarni
Greenhorn

Joined: May 22, 2013
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
prashantpk kulkarni
Greenhorn

Joined: May 22, 2013
Posts: 7
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

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Deleting the Files from Directory
 
Similar Threads
A Mirroring Algorithm
JTextArea has to be updated when copying directories and files
Recursive call problem
java.io.FileNotFoundException
Sometime not deleting files