Link to home
Start Free TrialLog in
Avatar of smithc
smithc

asked on

FileInputStreams

I have a bunch of xml files in a directory. I do not know the names nor do I know the number of files.  How do I obtain FileInputStreams to each of these files.  i.e., Suck in all *.xml files in a given directory. Then create a FileInputStream for each.

How do I do this?  I need this to work on both Solaris and Windows..

Thanks
Avatar of cheekycj
cheekycj
Flag of United States of America image

DUPLICATE QUESTION.

Experts please post all comments here:
https://www.experts-exchange.com/questions/20317040/FileInputStreams.html

DO NOT POST COMMENTS here.

smithc: Due to multiple submissions this question was created 3 times instead of once.  Please delete this one and the second one.

Refer to the link above as your main question.

Thanx,
CJ
Avatar of CEHJ
The answer that was accepted to this question had one drawback - it iterated *all* files in the directory, irrespective of whether they were xml files or not. But more important than this small inefficiency is that this is a problem crying out for a reusable dedicated class, as it's something that you're likely to do again and again. Here is such a class. All you need to do is override onFile(File f):

import java.io.*;

public class FileLister implements FileFilter {
     private boolean isCaseInsensitive;
     private String extension;
     
     public FileLister(String extension){
          this.extension = extension;
     }
     
     public FileLister(String extension,boolean isCaseInsensitive){
          this.extension = extension;
          this.isCaseInsensitive = isCaseInsensitive;
     }
     
  void iterate(String startDir){
    File dir = new File(startDir);
    // Make sure it *is* a directory
    if (dir.isDirectory()){
      File[] files = dir.listFiles(this);
               if (files.length > 0){
                    for(int i = 0;i< files.length;i++){
                         onFile(files[i]);
                    }
               }
    }    
  }
     
  public boolean accept(File pathname){
          if (isCaseInsensitive){
      return pathname.getName().toLowerCase().endsWith(extension.toLowerCase());
          }
          return pathname.getName().endsWith(extension);
  }
     
     public void onFile(File f){
          // Override this is a subclass (you could also use an Observer pattern here instead)
       System.out.println(f.getName());
          int c = -1;
          FileInputStream in = null;
          try {
            in = new FileInputStream(f);
               while((c = in.read()) > -1){
                    System.out.print((char)c);
               }
          }
          catch(IOException e){
               e.printStackTrace();
          }
          finally { try { in.close(); } catch(IOException e){ e.printStackTrace(); } }
     }
     
     public static void main(String[] args){
          FileLister fl = new FileLister(".xml",true);
          fl.iterate(".");
     }
     
}
   // Override this is a subclass
should have been:
   // Override this in a subclass
CEHJ: You should post in the other question still.. Since this is a dup and is supposed to be deleted.  No sense in having smithc pay double the points.  We can have a moderator split points if need be.

CJ
Don't worry about the points cheekycj. Couldn't post in the other question - it'd been closed.
you should still be able to add comments.. thats interesting.

CJ
ASKER CERTIFIED SOLUTION
Avatar of Moondancer
Moondancer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial