Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FileInputStreams

Posted on 2002-06-27
7
Medium Priority
?
265 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
[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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

721 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