• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

Java Prog to List file names ending with DAT

Can someone please help me with this.

I need help with a program which would look into a Directory and its sub-directories and list all the file names ending with *.dat

0
mkhan900
Asked:
mkhan900
6 Solutions
 
CEHJCommented:
Use the last example and write a method

private void process(File file) {
}
0
 
mkhan900Author Commented:
Sorry that did not help out much
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ariohCommented:
import java.io.File;

public class DATFinder {

  public static void main( String[] a ) {
    visitAllFiles( new File( "c:\\temp" ) );
  }

  public static void visitAllFiles( File dir ) {
    if ( dir.isDirectory() ) {
      String[] children = dir.list();
      for ( int i = 0; i < children.length; i++ ) {
        visitAllFiles( new File( dir, children[ i ] ) );
      }
    } else {
     process( dir );
    }
  }

  private static void process( File F ) {
    if ( F.getName().endsWith( ".dat" ) )
      System.out.println( F.getName() );
  }

}
0
 
ksivananthCommented:
here is an example using FileNameFilter for getting file(.mpeg/.xml ),

http://kickjava.com/57.htm
0
 
paddycobbettCommented:
A  more compact version of the above solution would be:

import java.io.*;

public class DatFilePrinter{

   public static void main(String args[]){
      printDatFiles(new File(args[0]));
   }

   public static void printDatFiles(File f){

      f.listFiles(new FileFilter(){
          public boolean accept(File f){
             if (f.isDirectory()) printDatFiles(f);
             else if (f.getName().endsWith(".dat")) System.out.println(f.getName());
             return true;
          }
         });
   }
}

To find .dat files in the current and sub directories, from the command line you would call:

java DatFilePrinter .

=)
0
 
CEHJCommented:
>>Sorry that did not help out much

What was the problem?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now