java代码如何将一个字节数组写入到文件中呢?
下文笔者讲述将字节数组写入到文件中的方法分享,如下所示
字节数组写入到文件的实现思路
1.定义一个文件输出流 2.将字节数组写入到流中 即可实现字节数组写入到文件的效果例
/**
* 将字节数组写入文件
* @param 字节狐族
* @throws Exception
*/
private static void byte2File(byte[] byt) throws Exception {
if (null == byt) {
return;
}
String targetFile = "D:\\test.txt";
File file = new File(targetFile);
OutputStream output = new FileOutputStream(file);
BufferedOutputStream out = new BufferedOutputStream(output);
InputStream in = new ByteArrayInputStream(byt);
byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
in.close();
out.close();
System.out.println("---- 写入完毕!----");
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


