java 文件和byte 互转
2024-10-20 05:37:59
/**
* 获得指定文件的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();
}
}
}
}
最新文章
- 北京培训记day4
- 前端试题本(HTML+CSS篇)
- Android APP测试的日志文件抓取
- JQuery ajax url传值与data传值的区别
- Tomcat上的项目部署到WebLogic上の注意事项
- windows phone listbox虚拟化(下)
- 《Cortex-M0权威指南》之Cortex-M0编程入门
- hibernate配置文件详细解释
- Java面试10|数据库相关
- Yum -userguide
- jQuery给表单设置值
- UNIX网络编程——并发服务器(TCP)
- Java IO学习--(四)网络
- mysql 在visual studio中的配置
- css学习-》养成路线
- 谷歌浏览器中安装Axure扩展程序
- JVM五大知识点
- http 本地服务器设置任意IP访问对应的文件夹
- 远程桌面连接工具 Remote Desktop Manager 9.1.2.0 Enterprise 多国语言绿色版附注册码 简单使用
- 负载均衡之-LVS