简体   繁体   中英

How to build FileInputStream object with byte array as a parameter

I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object. I dont want to create a file instead pass data content do FileInputStream. Is there any way ?

following is the code:

byte[] decodedHeaderFileZip = decodeHeaderZipFile(headerExportFile);
FileInputStream fileInputStream = new FileInputStream(decodedHeaderZipFileString);

EDIT: I wanted to build a ZipInputStream object with a FileInputStream.

I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object.

But you don't have a file. You have some data in memory. So a FileInputStream is inappropriate - there's no file for it to read from.

If possible, use a ByteArrayInputStream instead:

InputStream input = new ByteArrayInputStream(decodedHeaderFileZip);

Where possible, express your API in terms of InputStream , Reader etc rather than any specific implementation - that allows you to be flexible in which implementation you use. (What I mean is that where possible, make method parameters and return types InputStream rather than FileInputStream - so that callers don't need to provide the specific types.)

If you absolutely have to create a FileInputStream , you'll need to write the data to a file first.

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