aspose file tools*
The moose likes I/O and Streams and the fly likes Delete files in a directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Delete files in a directory" Watch "Delete files in a directory" New topic
Author

Delete files in a directory

bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
I nned to delete files in a specific directory. Could anyone tell me what's wrong with my code. Thanks.
import java.io.*;
public class FileNav{
public static void main(String argv[]){
//File dir = new File(System.getProperty("user.dir"));
String[] filenames;
String[] files=null;
File f = new File("Download");
filenames = f.list();
for(int i=0; i< filenames.length; i++)
System.out.println(filenames[i]);
File fs= new File(filenames[i]);
fs.delete();
}

}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by bob, morkos:
I nned to delete files in a specific directory. Could anyone tell me what's wrong with my code. Thanks.
import java.io.*;
public class FileNav{
public static void main(String argv[]){
//File dir = new File(System.getProperty("user.dir"));
String[] filenames;
String[] files=null;
File f = new File("Download");
filenames = f.list();
for(int i=0; i< filenames.length; i++)
System.out.println(filenames[i]);
File fs= new File(filenames[i]);
fs.delete();
}

}

Try with this:
public static void main(String argv[]){
String[] filenames;
String[] files=null;
File f = new File("Download");
filenames = f.list();
for(int i=0; i< filenames.length; i++) { //Don't forget this!!
File fs= new File(f.getName() + File.separator + filenames[i]);
fs.delete();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Delete files in a directory