Wednesday, October 10, 2012

Delete all Files in a Folder

import java.io.File;
import java.io.IOException;

public class DeleteFolderFiles {

 public static void main(String[] args) throws IOException {

   String folder = "C:\\data";
   File file = new File(folder);
     System.out.println( file ) ;
       if( file.isDirectory() )
       {
           String list[] = file.list() ;
           if( list != null )
           {
                  int size = list.length;
                 for(int i=0;i<size; i++ )
                {
                     File f = new File(file.getPath() ,list[i]);
                    System.out.println("list = "+list[i]);
                    System.out.println(f.getAbsolutePath());
                    if(f.exists())
                              f.delete();
               } // end For Loop
          }// end  2nd If
      }// end Main If
      
 }// end Method
    
 
}// end Class

1 comment:

  1. The material and aggregation is excellent and telltale as comfortably. Pyramid Analytics

    ReplyDelete