java File和Byte[]数组 相互转换
public class Test {public static void main(String[] args){String filePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src\\1.docx";String outFilePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src";String outFileName = "2.docx";getFile(getBytes(filePath),outFilePath,outFileName);}// ----------------获得指定文件的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中文件与Byte数组相互转换的方法
JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...
- Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式)
Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式) 原文地址:http://alanland.iteye.com/admin/blogs/1600685(欢迎转载 ...
- Java 基础类型转换byte数组, byte数组转换基础类型
Java 基础类型转换byte数组, byte数组转换基础类型 Java类型转换 java类对象转化为byte数组
- JAVA获取文件byte数组并输出进行展示和文件下载
/** * 文件下载 */ @GetMapping(value = "/download") public void download(HttpServletResponse re ...
- Java 文件和byte数组转换
/** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File fi ...
- Java 图片与byte数组互相转换
//图片到byte数组 public byte[] image2byte(String path){ byte[] data = null; FileImageInputStream input = ...
- JAVA File转Byte[]
/** * 获得指定文件的byte数组 */ public static byte[] getBytes(String filePath){ byte[] buffer = null; try { F ...
- 转转转--Java File和byte数据之间的转换
package cn.iworker.file; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; ...
- Java官方操纵byte数组的方式
java官方提供了一种操作字节数组的方法——内存流(字节数组流)ByteArrayInputStream.ByteArrayOutputStream ByteArrayOutputStream——by ...
随机推荐
- 设计模式之模板方法模式 templateMethod
代码实现 public abstract class BankTemplateMethod { //具体方法 public void takeNumber(){ System.out.println( ...
- create vm
#!/bin/sh echo $# [ $# < ] && { echo "error" exit } instance_name=$ instance_ip ...
- Python读取不同文件夹下的图片并且分类放到新创建的训练文件夹和标签文件夹
在深度学习的训练时,经常会碰到训练的样本数据集和标签数据集是在一个文件夹中,这个时候我们就不得不进行一些数据的预处理和文件的分类,例如将训练(training data)数据集和标签数据集(label ...
- Linux开启MySQL远程连接
Linux开启MySQL远程连接的设置步骤 . MySQL默认root用户只能本地访问,不能远程连接管理MySQL数据库,那么Linux下如何开启MySQL远程连接?设置步骤如下: 1.GRANT命令 ...
- ssh 免交互登录 ,远程执行命令脚本。
##免交互SSH登录auto_login_ssh () { expect -c "set timeout -1; spawn -noecho ssh -o ...
- 201621123034 《Java程序设计》第8周学习总结
作业08-集合 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 答 ...
- 【bzoj4325】NOIP2015 斗地主(&“加强”版) 搜索
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- DB2设置code page(日文943)
为了便于 DB2 在执行 DB2 命令或语句之后显示错误.警告和指示性消息,必须安装您期望使用的语言的 DB2 消息文件集.因为 DB2 有基于语言分组的不同分发版,您必须验证安装 CD-ROM 上有 ...
- Codeforces Round #357 (Div. 2) C
C. Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 学习C++ -> 向量(vector)
一.向量的介绍 向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器. 与string相同, vector 同属于STL(Standard Template Lib ...