JAVA中文件与Byte数组相互转换的方法,如下:

 public class FileUtil {

     //将文件转换成Byte数组
public static byte[] getBytesByFile(String pathStr) {
File file = new File(pathStr);
try {
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();
byte[] data = bos.toByteArray();
bos.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} //将Byte数组转换成文件
public static void getFileByBytes(byte[] bytes, 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(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

JAVA中文件与Byte数组相互转换的方法的更多相关文章

  1. Java中字符串和byte数组之间的相互转换

    1.将字符转换成byte数组 String str = "罗长"; byte[] sb = str.getBytes(); 2.将byte数组转换成字符 byte[] b={(by ...

  2. object与byte[]的相互转换、文件与byte数组相互转换

    转载自   https://blog.csdn.net/scimence/article/details/52233656 object与byte[]互转 /// <summary> // ...

  3. JAVA中Base64和byte数组(byte[]) 相互转换

    Base64转byte[] byte[] bytes = DatatypeConverter.parseBase64Binary("base64字符串"); byte[]转base ...

  4. Java中文件与字节数组转换

    注:来源于JavaEye 文件转化为字节数组: http://www.javaeye.com/topic/304980 /** * 文件转化为字节数组 * * @param file * @retur ...

  5. java 中 image 和 byte[] 相互转换

      java 中 image 和 byte[] 相互转换可恶的…………其实也挺好的 只是把好不容易写出来的东西记下来,怕忘了…… 下面,我来介绍一个简单的 byte[] to image, 我们只需要 ...

  6. Java 文件和byte数组转换

    /** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File fi ...

  7. java File和Byte[]数组 相互转换

    public class Test { public static void main(String[] args){ String filePath = "E:\\softoon\\wor ...

  8. java中文件的I/O操作

    java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...

  9. Java将文件转为字节数组

    Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...

随机推荐

  1. Nginx配置反向代理

    Nginx可做web服务器,也可做负载均衡使用. 反向代理:应用服务器不直接提供服务,通过nginx服务器处理请求, 转发到代理服务器(Tomcat,Nginx,Apache等) 获取响应交给客户端, ...

  2. 简陋的swift carthage copy-frameworks 辅助脚本

    在看 carthage 时,其中需要配置一个 copy-frameworks 脚本,此脚本特殊的地方,需要手动填写 Input Files 和 Output Files.一个一个填写,很糟心~ 观察了 ...

  3. 第一个程序python.py

    print("hello word")print("hello hello")print("hello hello")print(" ...

  4. IdentityServer(14)- 使用EntityFramework Core配置和操作数据

    IdentityServer具有良好的扩展性,其中一个可扩展点是用于IdentityServer所需数据的存储机制. 本快速入门介绍了如何配置IdentityServer以使用EntityFramew ...

  5. mmap函数实现

    转自:https://www.cnblogs.com/huxiao-tee/p/4660352.htmlmmap是一种内存映射文件的方法,即将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址 ...

  6. 使用三种方法求解前N个正整数的排列

    本篇博文给大家介绍前N个正整数的排列求解的三种方式.第一种是暴力求解法:第二种则另外声明了一个长度为N的数组,并且将已经排列过的数字保存其中:第三种方式则采用了另外一种思路,即首先获取N个整数的升序排 ...

  7. 洛谷 2634&&BZOJ 2152: 聪聪可可【点分治学习+超详细注释】

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 3435  Solved: 1776[Submit][Status][Discuss ...

  8. Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  9. [51nod1213]二维曼哈顿距离最小生成树

    二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间的距离为:横纵坐标的差的绝对值之和,即:Abs(x1 - x2) + Abs(y1 - y2)(也称曼哈顿距离).求这N个点所组成的完全图 ...

  10. 2017ecjtu-summer training #3 POJ3264

                                                                                                        ...