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

Grab files without knowing the name.

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
sladej
Asked:
sladej
  • 3
  • 2
1 Solution
 
doronbCommented:
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
 
bobbit31Commented:
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
 
doronbCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sladejAuthor Commented:
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
 
sladejAuthor Commented:
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
 
doronbCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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