1. 程式人生 > >byte陣列和File,InputStream互轉

byte陣列和File,InputStream互轉

1、將File、FileInputStream 轉換為byte陣列:

    File file = new File("file.txt");

    InputStream input = new FileInputStream(file);

    byte[] byt = new byte[input.available()];

    input.read(byt);

2、將byte陣列轉換為InputStream:

    byte[] byt = new byte[1024];

    InputStream input = new ByteArrayInputStream(byt);

3、將byte陣列轉換為File:

    File file = new File('');

    OutputStream output = new FileOutputStream(file);

    BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);

    bufferedOutput.write(byt);