java :文件和byte数组转换
来源:互联网 发布:2016年7月中国经济数据 编辑:程序博客网 时间:2024/06/17 18:09
/**
* 获得指定文件的byte数组
*/
private byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
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();
}
}
}
}
* 获得指定文件的byte数组
*/
private byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
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();
}
}
}
}
阅读全文
0 0
- java 文件和byte数组相互转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- java :文件和byte数组转换
- 文件和byte数组之间相互转换
- java int和byte数组的转换
- java int 和 byte数组转换
- java ByteBuffer和byte 数组相互转换
- java File和Byte[]数组 相互转换
- 文件和byte数组
- 使用JAVA将File文件转换为byte[]数组
- java 文件操作之byte数组间的转换
- java 实现 图片文件 与 byte 数组互相转换
- 图片文件和byte字节数组的转换
- Maven学习笔记
- Unity 导入下载音效出错
- mysql 取每个分组中时间最近的记录
- linux中daemon的改动之systemctl替代chkconfig和service
- 每日英语好文翻译2
- java :文件和byte数组转换
- [Android]查看代码混淆后的日志
- Java中 outer 标签的使用
- 通过java调用Http接口上传图片到服务器
- Oracle存储过程基本语法介绍
- django+spider简易爬虫搭建
- Parameter 'id' not found. Available parameters are [0, 1, 2, param3, param1, param2]
- 解决XSS攻击漏斗的过滤器
- 两直线四坐标点判断相交并求交点