Solved

FileInputStreams

Posted on 2002-06-27
7
211 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
Comment Utility
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
Comment Utility
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
Comment Utility
   // Override this is a subclass
should have been:
   // Override this in a subclass
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 19

Expert Comment

by:cheekycj
Comment Utility
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
Comment Utility
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
Comment Utility
you should still be able to add comments.. thats interesting.

CJ
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

6 Experts available now in Live!

Get 1:1 Help Now