简体   繁体   中英

How to get the InputStream from a files list of rtf document files?

I have a list of richTextFormat documents that I need to loop through and get the InputStream from each file to merge with another document. I used this code to get the files:

List<File> rtfFilesList = new ArrayList<File>();
File[] files = new File(<path to files>).listFiles();

for (File file : files) {
if (file.isFile() && file.getName().endsWith(".rtf")) {
    FilenameUtils.removeExtension(".rtf");
rtfFilesList.add(file);
 }
}

That gets me the files fine, but now I want to loop through the list, as in:

for(File file : rtfFilesList){
//Get the document stream from each file here.
...

I don't know how to get the File cast to a specific type of file. API didn't seem to really support that.

Any ideas on this one?

Thanks, James

Okay, I wasn't doing this quite right. What I really needed was:

for(File file : rtfTemplateList){
try {
    fileInputStream = new FileInputStream(file);
byte[] byteArray = new byte[(int) file.length()];
fileInputStream.read(byteArray);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
etc...

What I found after writing down what I wanted to do was really get a byteArrayInputStream to pass to a third party software generator to create a merged rtf file. So using the file in a for loop was correct, but then I just needed to get the stream after that.

I hope this helps someone.

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