Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2008

Tip: Looking for answers? Try searching our database.

listing dirs in current webapp...

Thread view: 
maya - 19 Feb 2008 16:15 GMT
hi, I'm trying to detect subdirectories in current webapp, not sure how
to filter dirs from files that are not dirs..  found something here,
http://www.exampledepot.com/egs/java.io/GetFiles.html?l=rel

 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];
        }
    }

but it how do you tell it to filter dirs ONLY?????
  (once we do String[] children = dir.list(); we can't test for
isDirectory() anymore, since isDirectory() method cannot be applied to
strings..)

am simply trying to detect which files in current dir (i.e., current
webapp) are directories..

thank you...
Lew - 19 Feb 2008 16:45 GMT
> hi, I'm trying to detect subdirectories in current webapp, not sure how
> to filter dirs from files that are not dirs..  found something here,
> http://www.exampledepot.com/egs/java.io/GetFiles.html?l=rel
...
> am simply trying to detect which files in current dir (i.e., current
> webapp) are directories..

<http://java.sun.com/javase/6/docs/api/java/io/File.html#isDirectory()>

You can get a list from File by using a FileFilter
<http://java.sun.com/javase/6/docs/api/java/io/FileFilter.html>

<snippet>
package snippet;
public class Snippet
{
 public static void main( String [] args )
 {
  File test = new File( args [0] );
  if ( test.isDirectory() )
  {
    File [] subDirs = test.listFiles(
      new FileFilter ()
      {
        public boolean accept( File t )
        {
         return t.isDirectory();
        }
      } );
    for ( File t : subDirs )
    {
      System.out.println( t.getName() );
    }
  }
 }
}
</snippet>

(NPEs ignored.)

Signature

Lew

Nigel Wade - 19 Feb 2008 17:42 GMT
> hi, I'm trying to detect subdirectories in current webapp, not sure how
> to filter dirs from files that are not dirs..  found something here,
[quoted text clipped - 21 lines]
>
> thank you...

Well, you could use the File(String) constructor and create a new File and
invoke the File.isDirectory() method on that. Or you could read the Javadocs
for File and find that it has a method listFiles() which returns a File[]
rather than a String[] ;-)

    File[] children = dir.listFiles();
    if (children == null) {
         // Either dir does not exist or is not a directory
    } else {
       for (File child:children) {
         if ( child.isDirectory() ) {
           // process the directory
         }
       }
    }

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

maya - 25 Feb 2008 20:20 GMT
>> hi, I'm trying to detect subdirectories in current webapp, not sure how
>> to filter dirs from files that are not dirs..  found something here,
[quoted text clipped - 37 lines]
>         }
>      }

yes, that was it..  thank you very much...
  (have never seen this construction..

   for (File child:children) {

couldn't use it in the end b/c needed standard 'i' var that's
declared/init'd in loops..  but curious anyway..

thank you..
Lew - 26 Feb 2008 02:37 GMT
>   (have never seen this construction..
>
>    for (File child:children) {
>
> couldn't use it in the end b/c needed standard 'i' var that's
> declared/init'd in loops..  but curious anyway..

This form of the 'for' loop was introduced in Java 5 (1.5), and is called the
'for-each' loop, or the 'enhanced-for' loop.
<http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html>

From the JLS
<http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2>
> The enhanced for statement has the form:
> EnhancedForStatement:
[quoted text clipped - 3 lines]
> or else it must be of an array type (§10.1),
> or a compile-time error occurs.

In your example, 'children' is the Expression, and is either an array,

File [] children;

or some Iterable such as a List <File>.

The 'for-each' loop is shorthand for, and works pretty near the same as, the
equivalent "classic" 'for' loop (where 'children' is an Iterable <File>):

 for ( Iterator <File> iter = children.iterator(); iter.hasNext(); )
 {
   File child = iter.next();
   doSomethingWith( child );
 }

For situations where, as you say, one needs the index, the 'for-each' isn't
the right idiom.  It is merely a shortcut around the Iterator, suitable when
an Iterator would have been involved.  The 'for-each' focuses on the value
returned at each iteration, the classic 'for' on position within the overall
collection.  Depending on which semantics suit the algorithm better, one
chooses the corresponding idiom.

Classic 'for' also has some lovely and esoteric powers far beyond the ken of
'for-each'.  My favorite is
  for ( ; ; ) { ... }

<http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.1>

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.