转转转--Java File和byte数据之间的转换
package cn.iworker.file; import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class FileTest
{ public static void main(String[] args)
{
file2BetyArray();
fileToBetyArray();
BetyToFile("D:\\Study\\Java\\First.class");
} public static byte[] file2BetyArray()
{
File file = new File("D:\\Study\\Java\\First.class");
if (!file.exists()) {
return null;
}
FileInputStream stream = null;
ByteArrayOutputStream out = null;
try {
stream = new FileInputStream(file);
out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1) {
out.write(b, 0, n);
}
return out.toByteArray();// 此方法大文件OutOfMemory
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
stream.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
} }
return null;
} public static byte[] fileToBetyArray()
{
FileInputStream fileInputStream = null;
File file = new File("D:\\Study\\Java\\First.class");
byte[] bFile = null;
try {
bFile = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
for (int i = 0; i < bFile.length; i++) {
System.out.print((char) bFile[i]);
}
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
bFile.clone();
} catch (IOException e) {
e.printStackTrace();
}
}
return bFile;
} public static File BetyToFile( String filePath)
{
File file = new File(filePath);
BufferedOutputStream stream = null;
FileOutputStream fstream = null;
byte[] data=new byte[(int)file.length()];
try {
fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
if (null != fstream) {
fstream.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
return file;
} }
public static void byte2File(byte[] buf, 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 + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
}
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 File和byte数据之间的转换的更多相关文章
- Java 中基本类型和字符串之间的转换
Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...
- Java学习--Java 中基本类型和字符串之间的转换
Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...
- Java中字节与对象之间的转换
近期公司里面用到了消息队列,而正如我们知道的是消息队列之间的是通过二进制形式的.以下就分享一下java中字节与对象之间的转换. 主要是用到了ByteArrayOutputStream和ObjectOu ...
- JavaBean和json数据之间的转换(一)简单的JavaBean转换
1.为什么要使用json? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,因为其高性能.可读性强的原因,成为了现阶段web开发中前后端交互数据的主要数据 ...
- C#字符串和数据之间的转换
c#中不仅仅存在数值类型的数据之间的转换,字符串和数值之间也是可以互相转换的,只是方法不同而已. 1 数值型转换为字符型 数值型数据转换为字符串用ToString()方法即可实现 int num1=1 ...
- JAVA中list,set,数组之间的转换详解
JAVA的list,set,数组之间的转换,主要是使用Apache Jakarta Commons Collections,具体的方法如下:import org.apache.commons.coll ...
- Java 集合 集合与数组之间的转换
Java 集合 集合与数组之间的转换 @author ixenos 数组转集合 Arrays.asList(T... a) 先给结论:用 Arrays.asList(T... a) 将数组转换成集合 ...
- java中Integer 和String 之间的转换
java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...
- Java开发学习--Java 中基本类型和包装类之间的转换
Java 中基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之 ...
随机推荐
- linux内核源码在线浏览
1.https://elixir.bootlin.com (只能搜索函数和宏定义,功能单一) 2.https://lxr.missinglinkelectronics.com (比第一个功能多一些, ...
- exception disappear when forgot to await an async method
https://github.com/aspnet/AspNetWebStack/issues/235 https://stackoverflow.com/questions/5383310/catc ...
- kylin构建cube优化
前言 下面通过对kylin构建cube流程的分析来介绍cube优化思路. 创建hive中间表 kylin会在cube构建的第一步先构建一张hive的中间表,该表关联了所有的事实表和维度表,也就是一张宽 ...
- 51Nod 1419 最小公倍数挑战
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1419 题意: 思路: 要想最大,肯定去找尽量大的互质的数,如果不是互质的 ...
- 癌症免疫细胞治疗知识:CAR-T与TCR-T的区别在哪里?--转载
肿瘤免疫治疗,实际上分为两大类.一种把肿瘤的特征“告诉”免疫细胞,让它们去定位,并造成杀伤:另一种是解除肿瘤对免疫的耐受/屏蔽作用,让免疫细胞重新认识肿瘤细胞,对肿瘤产生攻击(一般来说,肿瘤细胞会巧妙 ...
- lua if 流程控制
Lua认为false和nil为假,true和非nil为真. 要注意的是Lua中 0 为 true --[ 为 true ] ) then print("0 为 true") end ...
- 指定html转pdf文档
1.资源 <script type="text/javascript" src="./js/canvg2.js"></script> & ...
- Linux 最好是禁用IPV6
看着不爽, 还容易出事. 编辑文件 – /etc/sysctl.conf $ sudo gedit /etc/sysctl.conf 在文件的最后加入下面的行. # IPv6 disabled net ...
- hive row_number等窗口分析函数
一.排序&去重分析 row_number() over(partititon by col1 order by col2) as rn 结果:1,2,3,4 rank() over(parti ...
- hdu4990矩阵快速幂
就是优化一段代码,用矩阵快速幂(刚开始想到了转移矩阵以为是错的) 在搜题解时发现了一个神奇的网站:http://oeis.org/ 用来找数列规律 的神器.... 规律就是an=an-1+2*an-2 ...