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. pch文件的配置与路径修改

  2. switch case多值匹配

    switch case多值匹配一般有两种情况 1.列举(将所有值列举出来) var n= 3;switch (n){    case 1:     case 2:    case 3:         ...

  3. sqlserver2008行锁

    select * from tablename WITH (UPDLOCK) where Id=#value#

  4. KVO 进阶

    Key-value coding (KVC) 和 key-value observing (KVO) 是两种能让我们驾驭 Objective-C 动态特性并简化代码的机制.在这篇文章里,我们将接触一些 ...

  5. libMobileGestalt与UDID

    libMobileGestalt与UDID 没有评论 在iOS中,libMobileGestalt动态库, 用来取得各种系统变量,比如UDID, 磁盘使用量, 设备版本 在iOS7中,对于开发者来说, ...

  6. AutoTile 自动拼接(二) 学习与实践

    开始代码前,我们要做点准备工作. 下面 跟着我做. 首先我 扣了一个 图. 这个是 做 水的资源,所以是动态的,我把其余两张也扣了出来. 看起来一样,不是,这样看肯定 看不出所以然,你们先放到u3d中 ...

  7. AS3条件编译

    package { import flash.display.Sprite; public class Main extends Sprite { public function Main() { s ...

  8. HDU 3696 Farm Game

    SPFA最长路,思路如下: 先对题目中给出的每条边建边,权值为转化率:增加一个终点S,每个节点到S建边,权值为该物品的单价. 假设X物品最终转化为了Y物品,那么转化之后得到的钱就是 W[x]*转化率1 ...

  9. Angular服务器通信之:$http

    $http服务提供了浏览器XMLHttpRequest对象的封装,并且作为Angular中和后台服务通信的底层服务,在此之上Angular还提供了一个可选模块ngResource支持与RESTFul的 ...

  10. shell编程——if语句【转载】

    (2)shell编程——if语句_macg_新浪博客http://blog.sina.com.cn/s/blog_6151984a0100ekl6.html shell编程——if语句转载 if 语句 ...