Solved

Grab files without knowing the name.

Posted on 2002-06-12
6
221 Views
Last Modified: 2010-03-31
I have an application that reads a simple flat file with a pre-determined name and processes the information.  Now the customer needs the application to process multiple flat files.  I will not know the names of the files.  I will only know the folder location, where they reside, and can tell if I have the correct file by the first line of the file.  Does anyone know how to sequentially grab/open files in a folder when the number of files in the folder and the names are not known.

thanks,

sladej
0
Comment
Question by:sladej
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:doronb
ID: 7073811
Hi,


The java.io.File class in Java has a method called "listFiles()" that returns an array of java.io.File objects. You can use that to get the list of files/folders under a given folder.


Hope this helps,

Doron
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7074012
File f = new File ("<path to your directory>");

if (f.isDirectory()) {

     File[] files = f.listFiles();
     
     for (int i=0;i<files.length;i++) {
          File tmpFile = files[i];

          // read in first line from file
          String s;
          BufferedReader d
               = new BufferedReader(new FileReader(tmpFile));
          if ((s=d.readLine()) != null) {
               // check what you need to here
          }
     }
}
0
 
LVL 9

Expert Comment

by:doronb
ID: 7074821
Hi,


I use the following code to get a Vector of Files:

     public static Vector getFileList(File folder, Object oFilter) {
          Vector vFiles = new Vector();
          populateFileList(folder, vFiles, oFilter);
          return vFiles;
     }

     private static void populateFileList(File folder, Vector vFiles, Object oFilter) {
          File[] fileList = null;
          if (oFilter == null) {
               fileList = folder.listFiles();
          } else if (oFilter instanceof FileFilter) {
               fileList = folder.listFiles((FileFilter)oFilter);
          } else if (oFilter instanceof FilenameFilter) {
               fileList = folder.listFiles((FilenameFilter)oFilter);
          } else {
               return;
          }
          for (int i = 0; i < fileList.length; i++) {
               if (fileList[i].isDirectory()) {
                    populateFileList(fileList[i], vFiles, oFilter);
               } else {
                    vFiles.addElement(fileList[i]);
               }
          }
     }

It enables me to get a list of files contained in folder and all of it sub-folders.


Hope it helps

Doron
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:sladej
ID: 7078420
doronb,

I am trying your code and getting the following error.  Might you know the problem?

C:\BMT\com\uhc\renewals>java FileNameReader
The oFilter null
The elements are 31
The file2 is C:\BMT\com\uhc\renewals\BMTRenewal.class
Exception in thread "main" java.lang.NullPointerException
        at FileNameReader.populateFileList(FileNameReader.java:53)
        at FileNameReader.getFileList(FileNameReader.java:30)
        at FileNameReader.main(FileNameReader.java:17)


Here is the Code:


import java.io.*;
import java.sql.*;
import java.util.*;

  public class FileNameReader {
     
     Vector vFiles = new Vector();
     File[] fileList = null;

     public static void main (String[] args)
      {
          Vector vFile1 = new Vector();
          Object oFilter = new Object();
          FileNameReader f = new FileNameReader();
          File location = new File("C:\\BMT\\com\\uhc\\renewals\\");
        vFile1 = f.getFileList(location,oFilter);

     }

     public FileNameReader()
       {
          vFiles = null;
       }

     public Vector getFileList(File folder, Object oFilter)
     {
         //vFiles = null;
           oFilter =null;
         populateFileList(folder, vFiles, oFilter);
         return vFiles;
    }

     private void populateFileList(File folder, Vector vFiles, Object oFilter) {
         //File[] fileList = null;
            System.out.println("The oFilter "+oFilter);
         if (oFilter == null) {
              fileList = folder.listFiles();
         } else if (oFilter instanceof FileFilter) {
              fileList = folder.listFiles((FileFilter)oFilter);
         } else if (oFilter instanceof FilenameFilter) {
              fileList = folder.listFiles((FilenameFilter)oFilter);
         } else {
              return;
         }
           System.out.println("The elements are "+fileList.length);
         for (int i = 0; i < fileList.length; i++) {
              if (fileList[i].isDirectory()) {
                      System.out.println("The file is "+fileList[i]);
                   //populateFileList(fileList[i], vFiles, oFilter);
              } else {
                      System.out.println("The file2 is "+fileList[i]);
                   vFiles.addElement(fileList[i]);
              }
           Enumeration enum1 = vFiles.elements();
           System.out.println("The elements are "+vFiles.size());
         while (enum1.hasMoreElements()) {
              //Enumeration enum2 = (enum1.nextElement()).elements();
              while (enum1.hasMoreElements()) {
                   System.out.println(enum1.nextElement() + "--");
              }
              //System.out.println();

         }
    }
     }
  }
0
 

Author Comment

by:sladej
ID: 7078435
doronb,

I am trying your code and getting the following error.  Might you know the problem?

C:\BMT\com\uhc\renewals>java FileNameReader
The oFilter null
The elements are 31
The file2 is C:\BMT\com\uhc\renewals\BMTRenewal.class
Exception in thread "main" java.lang.NullPointerException
        at FileNameReader.populateFileList(FileNameReader.java:53)
        at FileNameReader.getFileList(FileNameReader.java:30)
        at FileNameReader.main(FileNameReader.java:17)


Here is the Code:


import java.io.*;
import java.sql.*;
import java.util.*;

  public class FileNameReader {
     
     Vector vFiles = new Vector();
     File[] fileList = null;

     public static void main (String[] args)
      {
          Vector vFile1 = new Vector();
          Object oFilter = new Object();
          FileNameReader f = new FileNameReader();
          File location = new File("C:\\BMT\\com\\uhc\\renewals\\");
        vFile1 = f.getFileList(location,oFilter);

     }

     public FileNameReader()
       {
          vFiles = null;
       }

     public Vector getFileList(File folder, Object oFilter)
     {
         //vFiles = null;
           oFilter =null;
         populateFileList(folder, vFiles, oFilter);
         return vFiles;
    }

     private void populateFileList(File folder, Vector vFiles, Object oFilter) {
         //File[] fileList = null;
            System.out.println("The oFilter "+oFilter);
         if (oFilter == null) {
              fileList = folder.listFiles();
         } else if (oFilter instanceof FileFilter) {
              fileList = folder.listFiles((FileFilter)oFilter);
         } else if (oFilter instanceof FilenameFilter) {
              fileList = folder.listFiles((FilenameFilter)oFilter);
         } else {
              return;
         }
           System.out.println("The elements are "+fileList.length);
         for (int i = 0; i < fileList.length; i++) {
              if (fileList[i].isDirectory()) {
                      System.out.println("The file is "+fileList[i]);
                   //populateFileList(fileList[i], vFiles, oFilter);
              } else {
                      System.out.println("The file2 is "+fileList[i]);
                   vFiles.addElement(fileList[i]);
              }
           Enumeration enum1 = vFiles.elements();
           System.out.println("The elements are "+vFiles.size());
         while (enum1.hasMoreElements()) {
              //Enumeration enum2 = (enum1.nextElement()).elements();
              while (enum1.hasMoreElements()) {
                   System.out.println(enum1.nextElement() + "--");
              }
              //System.out.println();

         }
    }
     }
  }
0
 
LVL 9

Accepted Solution

by:
doronb earned 100 total points
ID: 7081180
Hi,


Try it with (oFilter = null)

Since you put an Object in oFilter which is neither FileFilter nor FilenameFilter, the code goes through the method and the File array fileList is not initialized and remains null. If you would try the code with a FileFilter instance, FilenameFilter instance or NULL as a parameter passed to "populateFileList(..)" the code would work.


Hope this helps,

Doron
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question