This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 18108
    
  39

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: 10912
    
  12

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deleting the Files from Directory
 
Similar Threads
Sometime not deleting files
JTextArea has to be updated when copying directories and files
A Mirroring Algorithm
java.io.FileNotFoundException
Recursive call problem