Home Java Java-get-example Java get File List
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java get File List

Advertisement
In this section, you will study how to retrieve all the files and folders from the specified directory. In the given example, we are providing the directory 'C:\\" to the constructor of class File.

Java get File List

     

In this section, you will study how to retrieve all the files and folders from the specified directory. In the given example, we are providing the directory 'C:\\" to the constructor of class File. 

file.listFiles()- This method  returns an array of  files  and folders present in the specified directory with their pathname.

file.length()- This method returns the length of the provided directory.

files[fileInList]. toString- This will display all the files of directory on the console. 

 

Here is the code of GetFileList.java

import java.io.*;
public class GetFileList
{
 public static void main(String args[]){
 File file = new File("C:\\");  
 File[] files = file.listFiles();  
 for (int fileInList = 0; fileInList < files.length; fileInList++)  
 {  
 System.out.println(files[fileInList].toString());  
 }  
 }
}

Output will be displayed as:

Download Source Code

 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2008

Ask Questions?    Discuss: Java get File List   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com