1. 程式人生 > >怎麼把檔案物件轉化為位元組流

怎麼把檔案物件轉化為位元組流

//建立位元組物件

 byte[] buffer = null;

File file = new File("d://檔案轉位元組測試文件.txt");

FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream(fis.available());
byte[] bytes = new byte[fis.available()];
int temp;
while ((temp = fis.read(bytes)) != -1) {
          baos.write(bytes, 0, temp);
}
fis.close();
baos.close();
buffer = baos.toByteArray();