ByteArryInputStream与ByteArryOutputStrea的使用
public class Bytesoutputstrem {
public static void main(String[] args) {
byte[] bytes=getBytes("D:\\a.txt");
toFile(bytes, "D:\\d.txt");
}
public static byte[] getBytes(String path) {
try {
InputStream in=new BufferedInputStream(new FileInputStream(new File(path)));
ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
byte[] bytes=new byte[1024];
int len=0;
while(-1!=(len=in.read(bytes))) {
outputStream.write(bytes, 0, len);
}
in.close();
return bytes;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void toFile(byte[] bytes,String path) {
try {
OutputStream output=new BufferedOutputStream(new FileOutputStream( new File(path)));
InputStream input=new BufferedInputStream(new ByteArrayInputStream(bytes));
byte[] buffer=new byte[1024];
int len=0;
while(-1!=(len=input.read(buffer))) {
output.write(buffer, 0, len);
}
output.flush();
output.close();
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}