Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FileInputStreams

Posted on 2002-06-27
7
Medium Priority
?
269 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
Technology Partners: 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!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

885 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