3 Replies Latest reply on Nov 28, 2007 3:42 PM by 807603

    directory Count

    807603
      hi,

      I'm trying to get my app... to open a directory (done that using JFileChooser) and give me back the number of files within that directory (cant do that).

      at the moment i can only get it to give me the name of the directory it has opened.

      thanks

      stevanicus
        • 1. Re: directory Count
          800308
          ... and here's one I prepared earlier ...
          package krc.utilz.io;
          
          import java.io.File;
          import java.io.PrintStream;
          import java.io.IOException;
          import java.util.Queue;
          import java.util.LinkedList;
          
          //import krc.utilz.Tracer;
          
          public abstract class Dirz 
          {
          
            //private static Tracer tracer = new Tracer(System.err);
          
            private static final String slash = System.getProperty("file.separator");
          
            /**
             * returns the canonical name of the first ocurrence of seekFilename in or
             * under the given directory, using a recursive depth first file algorithm.
             */
            public static String findFirst(File directory, String seekFilename) throws IOException {
              //tracer.debug("> "+directory);
              File[] files = directory.listFiles();
              if(files==null) return(null);
              for ( int i=0; i<files.length; i++ ) {
                if ( files.isFile() && files[i].getCanonicalPath().endsWith(seekFilename) ) {
          //tracer.debug("< "+directory+" -> "+files[i].getCanonicalPath());
          return(files[i].getCanonicalPath());
          }
          }
          for ( int i=0; i<files.length; i++ ) {
          if ( files[i].isDirectory() ) {
          String classFilename = findFirst(files[i], seekFilename);
          if(classFilename!=null) {
          //tracer.debug("< "+directory+" -> "+classFilename);
          return(classFilename);
          }
          }
          }
          //tracer.debug("< "+directory+" -> null");
          return(null);
          }

          /**
          * prints the canonical name of each ocurrence of seekFilename in or under
          * the given directory, using a recursive depth first file algorithm.
          */
          public static void findAll(File directory, String seekFilename) throws Exception {
          Dirz.findAll(directory, seekFilename, System.out, 0);
          }
          public static void findAll(File directory, String seekFilename, PrintStream out) throws Exception {
          Dirz.findAll(directory, seekFilename, out, 0);
          }
          public static void findAll(File directory, String seekFilename, PrintStream out, int depth) throws Exception {
          try {
          File[] files = directory.listFiles();
          Queue<File> dirs = new LinkedList<File>();
          if (files==null) return;
          for ( int i=0; i<files.length; i++ ) {
          //tracer.debug(files[i].toString());
          if (files[i]!=null) {
          if (files[i].isFile()) {
          if (files[i].getCanonicalPath().endsWith(slash+seekFilename) ) {
          out.println(files[i].getCanonicalPath());
          }
          } else if (files[i].isDirectory()) {
          dirs.add(files[i]);
          }
          }
          } //next file
          for (File dir=dirs.poll(); dir!=null; dir=dirs.poll() ) {
          findAll(dir, seekFilename, out, depth+1);
          }
          } catch (Exception e) {
          System.out.println("ERROR: Dirz.findAll: "+e+" "+e.getMessage()+" at directory="+directory+" depth="+depth);
          throw e;
          }
          }

          }
          • 2. Re: directory Count
            807603
            Hi,

            See File.list ()

            Hope that help,
            Jack
            • 3. Re: directory Count
              807603
              thanks