The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


RE: How to read a directory. Please help!!!

Posted by Kishori Sharan on July 24, 2000 at 2:25 PM


At the end I have given a short example of how to print all the files and directory names with file length.
To work with file system in java you need to create an object of File class which is defined in package. So first step will be to import* ; in your program. You can create a file object in different ways. One of them is passing the file path like
File myFile = new File ( "C:\\Cms\\Modem" ) ;
Don't forget to specify two slashes "\\" in "C:\\Cms\\Modem" because "\" is a special char in java and if you want to put it in a string you have to give two.
Now if you just want to read the names of all files and directory under myFile ( I mean C:\Cms\Modem which is being represented by myFile in our program )then just call the list ( ) method of File class which returns an array of String.
String[] allFilesDirs = myFile.list ( ) ;
Now you can print the names or whatever you want to do with your files and directory name syou can do using allFilesDirs String array. However, this is not what you want to do as stated in your question. You need to go down in the hierachy and read all the files in the diretory one level down. Then you will have to use listFiles ( ) method of File class which returns an array of File class objects.

File[] allFiles = myFile.listFiles ( ) ;
Now you got all files and directories under c:\Cms\Modem represented by an object of File class contained in allFiles array. Here, allFiles array may contain files too. But, you want to look into only directory like 1,2 ( c:\Cms\Modem\1 ... ). To test whether a File object represents a system file or directory you have methods isDirectory ( ) and isFile ( ) in File class which returns boolean.So, loop thru allFiles array , check whether the current file is a directory or a file if it is a directory then again you can call listfiles ( ) method on them which will give you file/directory list in C:\Cms\Modem\1... , then check for file and get the the length using length ( ) method of File class.
Hope this will help you.

////////////////////////////////// Example /////////
import* ;
class ReadFiles {
public static void main ( String[] args ) {

String filedir ;
// Create a file object for your root directory
File f1 = new File ( "C:\\kishori" ) ;

// Get all the files and directory under your diretcory
File[] strFilesDirs = f1.listFiles ( );

for ( int i = 0 ; i < strFilesDirs.length ; i ++ ) {
if ( strFilesDirs[i].isDirectory ( ) )
System.out.println ( "Directory: " + strFilesDirs[i] ) ;
else if ( strFilesDirs[i].isFile ( ) )
System.out.println ( "File: " + strFilesDirs[i] + " (" + strFilesDirs[i].length ( ) + ")" ) ;





Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us