Solved

Grab files without knowing the name.

Posted on 2002-06-12
6
232 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
Independent Software Vendors: 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jmeter usage 4 57
Running JavaFX on the Raspberry Pi 27 274
Coding for the first time 9 97
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 33
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
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 …
Suggested Courses

738 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