I have a file that can be any thing like ZIP, RAR, txt, CSV, doc etc. I would like to create a ByteArrayInputStream from it.
I'm using it to upload a file to FTP through FTPClient from Apache Commons Net.
Does anybody know how to do it?
For example:
String data = "hdfhdfhdfhd"; ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
My code:
public static ByteArrayInputStream retrieveByteArrayInputStream(File file) { ByteArrayInputStream in; return in; }
ByteArrayInputStream , of the Java IO API enables you to read data from byte arrays as streams of bytes. In other words, the ByteArrayInputStream class can turn a byte array into an InputStream.
The InputStream is an abstract class and ByteArrayInputStream is a concrete class of InputStream and offers its own implementation of the abstract idea given by (InputStream), In Addition : A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream.
Use the FileUtils#readFileToByteArray(File)
from Apache Commons IO, and then create the ByteArrayInputStream
using the ByteArrayInputStream(byte[])
constructor.
public static ByteArrayInputStream retrieveByteArrayInputStream(File file) { return new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With