• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple fileInputStreams  RSS feed

 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks,

So I was wondering, is there any way to read multiple files and spit out a big file out of them with fileInputStream? Right now I am just reading a file like;



and then on the command line by


what I am looking for is to make this able to read a directory per se, or at least could able to add more arg as readable files.

Thanks in advance
ilteris kaplan.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm pretty sure you'll need to make a list of files and iterate through them, reading them one at a time. Look at File.list() or listFiles().

And for fun, examine the args array in your main method if you enter:

java myprogram *.txt

The results may vary between Windows & Unix.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer depends a lot on how many files you are going to open and what you want to do with them. If you want to read two files (or some other known number), you can just create multiple instances of FileInputStream. For two, you would probably just use two separate instance variables. For more, you might want to use a Collection (probably a List, to be more specific) or an array. You need to be a little careful here since the operating system usually only allows a maximum number of files to be open at one time.

If you don't know the number of files you will open, you can still use a Collection. However, most likely you will want to only have one file open at a time, so a single reference variable should be enough. This will probably involve a loop of some kind so you can repeat the same actions for each file.

I'm sorry if this is a bit vague. If you need more help, please provide more specific details about what you want to do so we can answer your questions with more specific examples.

I hope this helps.

Layne
 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually what I am looking for is a generic approach where I can just pass the files names through a reference and sum them up in one big file and write this file in the end.

So in this case there is no specific number of files. it could be 2 or it could be 10. But in any case I don't mind passing the number of the source as an arg to the function.

and one other thing is I want to have full error handling which is an area I haven't covered in details in Java, can you guys point me to some sources about this, especially about file I/O programs.

best
ilteris.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you need to handle this one file at a time then. I don't think you should bother with a Collection or array. Perhaps it might be helpful to write a method that opens a file and appends it to another file. Then you can write another method that loops through a list of files. I can think of several ways to do this, but I will leave it up to you to figure out the exact details.

As for error handling, you should start with Handling Errors Using Exceptions from the Java Tutorial.

HTH

Layne
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A SequenceInputStream represents the logical concatenation of other input streams." -- API documentation for java.io.SequenceInputStream
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!