skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, November 2, 2012

Listing Files and Directory Using Java

Posted by Raju Gupta at 9:30 PM – 0 comments
 

Lists Files and directory present in the system   

File dir = new File("directoryName"); 
 String[] children = dir.list();
  if (children == null) 
{      
// Either dir does not exist or is not a directory 
 }
 else 
{   
   for (int i=0; i < children.length; i++) 
{
          // Get filename of file or directory       
   String filename = children[i];     
 }
  }
   // It is also possible to filter the list of returned files.
  // This example does not return any files that start with `.'.  
FilenameFilter filter = new FilenameFilter() 
{      
public boolean accept(File dir, String name)
 {
          return !name.startsWith(".");    
  }
  };
  children = dir.list(filter);
   // The list of files can also be retrieved as File 
File[] files = dir.listFiles();   
// This filter only returns directories  
FileFilter fileFilter = new FileFilter()
 {

      public boolean accept(File file)
 {
          return file.isDirectory();   
   }
  };  
files = dir.listFiles(fileFilter); 


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger