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数据之间的转换的更多相关文章

  1. Java 中基本类型和字符串之间的转换

    Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...

  2. Java学习--Java 中基本类型和字符串之间的转换

    Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...

  3. Java中字节与对象之间的转换

    近期公司里面用到了消息队列,而正如我们知道的是消息队列之间的是通过二进制形式的.以下就分享一下java中字节与对象之间的转换. 主要是用到了ByteArrayOutputStream和ObjectOu ...

  4. JavaBean和json数据之间的转换(一)简单的JavaBean转换

    1.为什么要使用json? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,因为其高性能.可读性强的原因,成为了现阶段web开发中前后端交互数据的主要数据 ...

  5. C#字符串和数据之间的转换

    c#中不仅仅存在数值类型的数据之间的转换,字符串和数值之间也是可以互相转换的,只是方法不同而已. 1 数值型转换为字符型 数值型数据转换为字符串用ToString()方法即可实现 int num1=1 ...

  6. JAVA中list,set,数组之间的转换详解

    JAVA的list,set,数组之间的转换,主要是使用Apache Jakarta Commons Collections,具体的方法如下:import org.apache.commons.coll ...

  7. Java 集合 集合与数组之间的转换

    Java 集合 集合与数组之间的转换 @author ixenos 数组转集合 Arrays.asList(T... a) 先给结论:用 Arrays.asList(T... a) 将数组转换成集合 ...

  8. java中Integer 和String 之间的转换

    java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...

  9. Java开发学习--Java 中基本类型和包装类之间的转换

    Java 中基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之 ...

随机推荐

  1. Java 时间格式处理

    jdk里面的日期格式处理使用SimpleDateFormat,这个类其实也是在内部调用的Calendar Calendar概念比较负责,涉及到时区和本地化 看一些简单的demo: package co ...

  2. 【Java----正则关键字转义】

    正则需要转义字符:'$', '(', ')', '*', '+', '.', '[', ']', '?', '\\', '^', '{', '}', '|'   问题现象:  替换URL格式的字符串时 ...

  3. java学习之浅谈多线程2--线程同步

    如果一个共享资源被多个线程同时访问,可能会遭到破坏.举个例子说明这个问题,假设创建并启动100个线程,每个线程都往同一个账户中添加一个便士,代码如下: import java.util.concurr ...

  4. java线程中的interrupt,isInterrupt,interrupted方法

    在java的线程Thread类中有三个方法,比较容易混淆,在这里解释一下 (1)interrupt:置线程的中断状态 (2)isInterrupt:线程是否中断 (3)interrupted:返回线程 ...

  5. 记一次加载js不全报错的原因总结

    1.运营商广告 dns 劫持2.浏览器本身并发数3.服务器配置keepAliveTimeout=04.加载文件过大5.

  6. UriComponentsBuilder和UriComponents url编码

    Spring MVC 提供了一种机制,可以构造和编码URI -- 使用UriComponentsBuilder和UriComponents. 功能相当于 urlencode()函数,对url进行编码, ...

  7. ADSL拨号上网或者光纤上网设置概要(原创)

    不管是在梧州设置光纤还是在太平设置ADSL拨号上网每次设置上网一体机的时候都是遇到各种麻烦...这次又是弄了N久,每次问题各不一样.总结一下操作过程,方便以后又遇问题回头查询自个微博.一.设置电话线的 ...

  8. 057——VUE中vue-router之路由参数默认值的设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. VS2017打包安装程序

    VS2017 并不自带安装部署项目,需要在[扩展和更新]中安装插件:Microsoft Visual Studio 2017 Installer Projects(现更名为Microsoft Visu ...

  10. mmap实现大文件快速拷贝

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...