怎么把XML内容直接转换成byte

如题所述

java将文件转换为byte数组,主要是使用输出流,实例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null; //新建一个输出流
FileOutputStream fos = null; //w文件包装输出流
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName); //新建一个file类
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos); //输出的byte文件
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close(); //关闭资源
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close(); //关闭资源
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答