http://blog.csdn.net/commonslok/article/details/9493531

public static byte[] File2byte(String filePath)
{
byte[] buffer = null;
try
{
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
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;
} 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();
}
}
}
}

File和byte[]转换的更多相关文章

  1. inputStream、File、Byte、String等等之间的相互转换

    一:inputStream转换 1.inputStream转为byte //方法一 org.apache.commons.io.IOUtils包下的实现(建议) IOUtils.toByteArray ...

  2. IFile、File与实体转换

    /** * 根据物理实体文件在开发工程中创建实体文件 */ @Override public void getEntityFilesByErFile(IFile erfile, IFolder ent ...

  3. file与 byte[] 互转

    byte 转file String filepath="D:\\"+getName();          File file=new File(filepath);        ...

  4. java byte数组与int,long,short,byte转换

    public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return conver ...

  5. Stream与byte转换

    将 Stream 转成 byte[] /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] Str ...

  6. byte数组与int,long,short,byte转换 (转载)

    byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteO ...

  7. 16进制到byte转换

    我们经常会看到这样的语法 (byte) 0xAD 0xAD实际是个16进制,转换成二进制为:10101101,转换成10进制是:173,它是个正数 10101101只是int的简写,int由4个byt ...

  8. C# byte[] 转换16进制字符串

    1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...

  9. byte数组转float实现与byte转换其它类型时进行&运算原理

    下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] &a ...

随机推荐

  1. 学习最短路建图 HUD 5521

    http://acm.hdu.edu.cn/showproblem.php?pid=5521 题目大意:有n个点,m个集合,每个集合里面的点都两两可达且每条边权值都是val,有两个人A, B,A在po ...

  2. pro文件常用内容

    qmake生成的pro文件中常用变量 SUBDIRS 指定子目录 TARGET 指定生成的应用程序名(默认为项目名) DEPENDPATH 指定程序编译时依赖的相关路径 INCLUDEPATH 指定头 ...

  3. docker镜像与容器概念

    本文用图文并茂的方式介绍了容器.镜像的区别和Docker每个命令后面的技术细节,能够很好的帮助读者深入理解Docker. 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(containe ...

  4. Glusterfs 分布式存储安装部署

    Glusterfs 分布式存储部署 是存储当中可以选择的一种 现在很多虚拟化 云计算都在用软件存储 例如 ceph Glusterfs 等等 今天我们部署一下Glusterfs环境 GlusterFs ...

  5. UITextfield的一些属性

    //设置左视图 不用设置frame UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&quo ...

  6. selenium2(WebDriver) API

    selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/  1.1  下载selenium2.0的包 官方downl ...

  7. perfect-scrollbar示例

    <!DOCTYPE html> <html> <head> <title>perfect-scrollbar - www.97zzw.com -97站长 ...

  8. less分页阅读

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  9. oracle 锁表的处理。

    最近系统每天经常锁表,进程杀死后,很快再次锁住这个表. (一)先贴出现场处理死锁的步骤. 另外:有时候通过PL/SQL执行kill session可能仍然无法解锁,此时需要登陆到Oracle服务器将进 ...

  10. Java NIO Related

    A file's status is 3-valued: The file is verified to exist; The file is verified to not exist; The f ...