Solved

Grab files without knowing the name.

Posted on 2002-06-12
6
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 73
hibernate insert example 13 38
Why my table column Id is not passed to java object? 4 46
dao vs facade design patterns 2 33
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

756 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