Solved

Grab files without knowing the name.

Posted on 2002-06-12
6
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:sladej
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
wordappend challenge 8 82
bitbucket vs gitbucket 3 28
eclipse compiler vs Installed JREs option 3 35
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…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now