获取文件的byte数组与根据byte数组生成文件的工具类示例
来源:互联网 发布:淘宝950轮播代码 编辑:程序博客网 时间:2024/05/20 19:16
public class ByteToFile {
//获得指定文件的byte数组
public static byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
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 File 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.mkdirs();
}
file = new File(filePath+File.separator+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
bos.flush();
} 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();
}
}
}
return file;
}
}
//获得指定文件的byte数组
public static byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
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 File 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.mkdirs();
}
file = new File(filePath+File.separator+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
bos.flush();
} 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();
}
}
}
return file;
}
}
阅读全文
0 0
- 获取文件的byte数组与根据byte数组生成文件的工具类示例
- byte数组与文件的转换
- 获取文件字节数组byte[]
- 文件转换成byte[]数组 / byte[]生成文件
- java读取文件读取工具类-将文件到byte数组的几种方式
- object与byte[]的相互转换、文件与byte数组相互转换
- 文件和byte数组
- byte[]数组工具类
- java byte数组与文件读写
- java byte数组与文件读写
- java byte数组与文件读写
- byte文件的获取函数
- unity byte数组存储为.byte文件
- Android中根据byte数组生成文件并保存到手机
- Android中根据byte数组生成文件并保存到手机
- 将byte数组写入文件
- Java的byte数组
- byte数组的转化
- Yii框架中增删改查
- Retrofit框架读书总结笔记(一)
- Java虚拟机学习(一)-运行时数据区
- 移植zlog到arm linux
- 卷积神经网络(三)
- 获取文件的byte数组与根据byte数组生成文件的工具类示例
- 机器学习工程师指南
- node学习笔记(二)(ajax方式向node后台提交数据)
- 文章标题
- 卷积神经网络(四)
- iOS-小总结
- js注释
- MyBatis-Generator在Eclipse上配置及使用
- 随机森林