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 ...
随机推荐
- xshell连接Ubuntu虚拟机
Ubuntu系统 1,安装ssh sudo apt-get install openssh-server 2,启动ssh进程 /etc/init.d/ssh start 3,查看进程信息 ps -e ...
- shell脚本自带变量的含义
$0 Shell本身的文件名 $1-$n 添加到Shell的各参数值.$1是第1参数.$2是第2参数… $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process ...
- linux系统定时任务设置
.使用at命令设置一次性定时任务 2.使用crontab设置周期性定时任务 1)cd /home 目录下,使用vi test.py创建文件,内容如下: #!/usr/bin/python#coding ...
- sqlserver如何添加全文索引
在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了. 下面简明的介绍如何使用Sql2008 全文索引 一.检查 ...
- 使用常见的网络命令查看当前网络状态——Mac OS X篇
转载自:http://blog.csdn.net/zkh90644/article/details/50539948 操作系统拥有一套通用的实用程序来查明本地主机的有线或者无线链路状态和IP的连接情况 ...
- python模块之xml.etree.ElementTree
xml.etree.ElementTree用于解析和构建XML文件 <?xml version="1.0"?> <data> <country nam ...
- vsftpd.log内容的意义
vsftpd日志(xferlog格式)的含义 引用: Thu Mar 4 08:12:30 2004 1 202.114.40.242 37 /incoming/index.html a _ o a ...
- python 面试
知识总结 面试(一)
- DB2:FETCH FIRST 1 ROWS ONLY
DB2:FETCH FIRST ROWS ONLY,在Oracle中使用where rownum=1代替 DB2: SELECT INSTORAGENO FROM ( SELECT max(AUDIT ...
- interesting Integers(数学暴力||数论扩展欧几里得)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8