Solved

FileInputStreams

Posted on 2002-06-27
7
241 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:smithc
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 7114658
DUPLICATE QUESTION.

Experts please post all comments here:
http://www.experts-exchange.com/java/Q_20317040.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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7114998
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(".");
     }
     
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7115018
   // Override this is a subclass
should have been:
   // Override this in a subclass
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 19

Expert Comment

by:cheekycj
ID: 7115019
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7115112
Don't worry about the points cheekycj. Couldn't post in the other question - it'd been closed.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7115121
you should still be able to add comments.. thats interesting.

CJ
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
ID: 7115132
smithc asked that this be deleted, but as you've added helpful information, I will refund 100 points and move this to the PAQ at zero.

smithc ->  You can always award CEHJ for help here by posting a new question in this topic area and entitle it:
Poits for CEHJ and in the comment field, paste this question link (URL).

Moondancer - EE Moderator
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
configure dependency in POM for new database 3 34
Unhandled exception type Exception 18 42
jmss example java 2 23
running on tomcat not jboss eap 7.0 3 18
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

829 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