Java中文件与字节数组转换
注:来源于JavaEye
文件转化为字节数组:
http://www.javaeye.com/topic/304980
- /**
- * 文件转化为字节数组
- *
- * @param file
- * @return
- */
- public static byte[] getBytesFromFile(File file) {
- byte[] ret = null;
- try {
- if (file == null) {
- // log.error("helper:the file is null!");
- return null;
- }
- FileInputStream in = new FileInputStream(file);
- ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
- byte[] b = new byte[4096];
- int n;
- while ((n = in.read(b)) != -1) {
- out.write(b, 0, n);
- }
- in.close();
- out.close();
- ret = out.toByteArray();
- } catch (IOException e) {
- // log.error("helper:get bytes from file process error!");
- e.printStackTrace();
- }
- return ret;
- }
字节数组转化为文件
http://www.javaeye.com/topic/304982
- /**
- * 把字节数组保存为一个文件
- *
- * @param b
- * @param outputFile
- * @return
- */
- public static File getFileFromBytes(byte[] b, String outputFile) {
- File ret = null;
- BufferedOutputStream stream = null;
- try {
- ret = new File(outputFile);
- FileOutputStream fstream = new FileOutputStream(ret);
- stream = new BufferedOutputStream(fstream);
- stream.write(b);
- } catch (Exception e) {
- // log.error("helper:get file from byte process error!");
- e.printStackTrace();
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e) {
- // log.error("helper:get file from byte process error!");
- e.printStackTrace();
- }
- }
- }
- return ret;
- }
Java中文件与字节数组转换的更多相关文章
- Java将文件转为字节数组
Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...
- JAVA中文件与Byte数组相互转换的方法
JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...
- java 读取文件的字节数组
/*文件64位编码*/ public static void main(String[] args) { byte[] fileByte = toByteArray(newFile); St ...
- Java中如何将字符串数组转换成字符串
如果将“字符串数组”转换成“字符串”,只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb ...
- java中如何将字符串数组转换成字符串(转)
如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...
- 【Java】字节数组转换工具类
import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...
- java中文件的I/O操作
java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...
- java对获取的字节数组进行处理
java对获取的字节数组bytes[]进行处理: 第一种,直接将该字节数组转换为字符串(部分): String content = ,); //从位置0开始获取2个字节 这样,对获取的数据报进行全部转 ...
- C#字节数组转换成字符串
C#字节数组转换成字符串 如果还想从 System.String 类中找到方法进行字符串和字节数组之间的转换,恐怕你会失望了.为了进行这样的转换,我们不得不借助另一个类:System.Text.Enc ...
随机推荐
- https、socket、http协议
一.https https 其实是由两部分组成:http+ssl(Secure Sockets Layer 安全套接层)/tls(Transport Layer Security 继任者安全传输层), ...
- HDU 6195 2017沈阳网络赛 公式
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6195 题意:有M个格子,有K个物品.我们希望在格子与物品之间连数量尽可能少的边,使得——不论是选出M个 ...
- Oracle常用sql语句(三)之子查询
子查询 子查询要解决的问题,不能一步求解 分为: 单行子查询 多行子查询 语法: SELECT select_list FROM table WHERE expr operator (SELECT s ...
- 表格中上移下移置顶的js操作
<script> $(function(){ //上移 var $up = $(".up") $up.click(function() { var $tr = ...
- 设计模式--工厂模式 caffe_layer注册
来源:http://www.cnblogs.com/zhouqiang/archive/2012/07/20/2601365.html 来源:http://blog.luoyetx.com/2016/ ...
- oracle创建计划任务
特别提示: oracle是执行完任务,才按照interval去计算下次执行时间!!! 为精确每个5分钟执行一个任务,必须自己计算时间. 如:trunc_minute(sysdate)+/ create ...
- Windows搭建python开发环境[一]
首先需要去python的官网下载环境.鼠标移动到Downloads的tab上,在这里可以下载. python的环境还是很人性化的,没有那么多罗里吧嗦的配置什么的,下载好以后直接无脑next就行了,直到 ...
- PHP using mcrypt and store the encrypted in MySQL
This is how I would do it. Create a class to do encryption/decryption: class cipher { private $secur ...
- TCP/IP——ARP与RARP简记
ARP(Address Resolution Protocol):ARP为IP地址到对应的硬件地址(MAC)之间提供动态映射.这个过程是自动完成的,一般应用程序用户和系统管理员不必要关心. ARP高速 ...
- open -python操作文件
一打开文件 二操作文件 三关闭文件 open(文件,模式,编码),打开文件----->0101010(以二进制的方式打开)------>编码(open默认utf-8编码)------> ...