private static void byteArrayOutputStream() throws FileNotFoundException,
IOException {
FileInputStream fis = new FileInputStream("a.txt");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
//写到字节数组
byte[] bytes = new byte[1024];
while((len=fis.read(bytes))!=-1){
bos.write(bytes);
}//写到内存,垃圾回收,没关联文件,无所谓关不关
System.out.println(bos);//对象自动调用toString()
/* while((len=fis.read())!=-1){
bos.write(len);
}
// byte[] bytes = bos.toByteArray();
// System.out.println(new String(bytes));
System.out.println(bos);*/
} |
|