文件输出流
public static void write(String filePath, byte[] source){
File newFile = new File(filePath);
if (!newFile.getParentFile().exists()) {
newFile.mkdir();
}
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
fileOutputStream = new FileOutputStream(newFile);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write(source);
bufferedOutputStream.flush();
bufferedOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedOutputStream != null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
生成文件(xml,text.....)
最新推荐文章于 2024-06-06 15:02:18 发布
本文介绍了一种使用Java进行文件输出流操作的方法,包括创建目录、使用FileOutputStream和BufferedOutputStream写入字节到指定文件,并确保资源正确关闭,以防内存泄漏。
2918

被折叠的 条评论
为什么被折叠?



