The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2000

Advertisement

Advertisement

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.

Message:

your error is not imporint util.java.* class

Posted by dhanyu on August 04, 2001 at 12:03 AM

type

import java.util.* in the program

it'll work fine

good luck


> > Hi

> > 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 java.io package. So first step will be to import java.io.* ; 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 java.io.* ;
> > 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 ( ) + ")" ) ;

> >
> > }
> > }

> > }

> > Thanx
> > Kishori

> Hi Kishori!!!

> Look I just want to thank you for responding to my plea. What I want to let you know
> is that I've tried the example you gave me and the compiler is moaning about the
> listFiles() method. It says that "Method listFiles() not found in the class
> java.io.File.

> Now I tried to look at some java manuals and I can't seem to get something that will
> help me in that regard. Could you maybe have a look at it again and maybe there's something
> that you've probably missed.

> Thank you again in advance.

> Thabani!!!






Replies:

Sponsored Links



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