1. 程式人生 > >Byte[] to InputStream or OutputStream

Byte[] to InputStream or OutputStream

byte[] source = ...;
ByteArrayInputStream bis = new ByteArrayInputStream(source);
// read bytes from bis ...

ByteArrayOutputStream bos = new ByteArrayOutputStream();
// write bytes to bos ...
byte[] sink = bos.toByteArray();

file path to inputstream:

InputStream is;

try {
is = new FileInputStream(“c://filename”);

is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return is;

File file=new File(“c:\\abc.txt”);
InputStream is = new FileInputStream(file);
or

InputStream is = new FileInputStream(“c:\\abc.txt”);