简体   繁体   中英

Is there an easy way to create a Java InputStream that consists of several appended files?

I have a "processor" component that can process a single File, InputStream, Reader, or etc.

For various reasons, I end up with several large files instead of one huge file.

Is there a way to construct an input stream (or reader) that: transparently "appends" all these files so that: 1) The "processor" does not know where one file started or another ended 2) No changes occur in the file system (eg, no actual appending of files) 3) Each file is read in order so that I do not pay the cost of loading all of them to memory and appending them before the processor starts reading?

I'm sure it is possible to write something like this, but I'm wondering if one exists already; it's been a while since I did file based IO.

SequenceInputStream concatenates multiple streams.

List<InputStream> opened = new ArrayList<InputStream>(files.size());
for (File f : files) 
  opened.add(new FileInputStream(f));
InputStream is = new SequenceInputStream(Collections.enumeration(opened));

Exception handling (not shown) when opening each file is important; be certain that all files are certain to be closed eventually, even if the operation is aborted before the SequenceInputStream is created.

您可以使用类似SequenceInputStream的内容来读取另一个流。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM