java File 与 byte[] 互转
来源:互联网 发布:吉他选购知乎 编辑:程序博客网 时间:2024/05/29 05:13
/**
* 获得指定文件的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(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数组
*/
public static 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();
}
}
}
}
- java File 与 byte[] 互转
- java string与byte[]互转
- java 对象与byte[]互转
- Image,File与Byte转换
- Java 中的 long 与 byte 互转
- java中byte与int互转
- java int 与byte互转(转)
- java中float与byte[]互转
- Java File,object,byte[]间转换
- java File和Byte[]数组 相互转换
- java InputStream,String,File byte相互转化
- JAVA 与 C# 通讯中的 int 与 byte 互转
- java int 与 byte转换
- Java中String与byte[]
- c#与java中的byte
- java int 与 byte[] 转化
- C# 与 JAVA 中的 byte
- java byte 与 binary 转换
- jsoncpp解析拼装数组
- linux v4l 编程 V4L 查询操作(代码参考)
- 一道题
- c语言详解sizeof
- 运维工程师必会的109个Linux命令
- java File 与 byte[] 互转
- 内容提供者---进阶
- wince应用程序(实现 获取存储设备的大小、实现 WINCE 系统重新启动、隐藏/显示 Windows 任务栏等等)【C#】
- 随笔:charactercontroller沿着斜坡前进
- 网络游戏开发概述以及开发岗位
- 在嵌入式Linux系统中应用的GTK+和X分析
- 自动化软件安全防护解决方案实例介绍
- ASP.NET导出EXCEL方法总结
- Objective C 享元模式