
java怎么把一个类转成byte数组
2个回答
2017-01-12
展开全部
java将文件转换为byte数组,主要是使用输出流,实例如下:
/**
* 根据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();
} 更多技术问题可在itjob技术群交流
}
}
}
/**
* 根据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();
} 更多技术问题可在itjob技术群交流
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询