byte数组和文件的相互转换
/**
* 获得指定文件的byte数组
*/ private byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
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;
}
/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, 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+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

byte数组和文件的相互转换的更多相关文章
- iOS-NSdata 与 NSString,Byte数组,UIImage 的相互转换
IOS---NSdata 与 NSString,Byte数组,UIImage 的相互转换 1. NSData 与 NSString NSData-> NSString NSString *aSt ...
- C# byte数组与Image的相互转换
功能需求: 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库. 2.把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示. 3.从图片byte ...
- ios -- NSdata 与 NSString,Byte数组,UIImage 的相互转换(转)
1. NSData 与 NSStringNSData-> NSStringNSString *aString = [[NSString alloc] initWithData:adata enc ...
- NSdata 与 NSString,Byte数组,UIImage 的相互转换
1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataen ...
- [C#参考]byte数组和Image的相互转换
功能需求 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组在内存中操作. 2.把内存中的byte数组转换成Image对象,赋值给相应的控件显示. 3.从图片byte数组得到 ...
- C# byte数组与Image的相互转换【转】
功能需求: 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库. 2.把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示. 3.从图片byte ...
- java byte数组与String的相互转换
String -> byte数组 String str = "abc天"; byte[] btr = str.getBytes(); System.out.printl ...
- C# Byte[]数组读取和写入文件
这个项目我用的是asp.net构建的,代码如下 protected void ByteToString_Click(object sender, EventArgs e) { string conte ...
- java byte【】数组与文件读写(增加新功能)
今天在测试直接写的文章: java byte[]数组与文件读写 时,想调用FileHelper类对字节数组以追加的方式写文件,结果无论怎样竟然数据录入不全,重新看了下文件的追加模式,提供了两种方式: ...
随机推荐
- [AtCoderContest015D]A or...or B Problem
[AtCoderContest015D]A or...or B Problem 试题描述 Nukes has an integer that can be represented as the bit ...
- POJ3349 Snowflake Snow Snowflakes 【哈希表】
题目 很简单,给一堆6元组,可以从任意位置开始往任意方向读,问有没有两个相同的6元组 题解 hash表入门题 先把一个六元组的积 + 和取模作为hash值,然后查表即可 期望\(O(n)\) #inc ...
- VCO的配置方法
弄了个VCO的环境. 感觉有点儿麻烦,配乱七八糟的服务,弄完了SE也不试试,白弄了.最近又有人说这东西要试试. 我先简单记录下吧: 1. 在vCenter Server 下开启SSO,设置密码. 2. ...
- Java面试题之Array和ArrayList的区别
Array和ArrayList的区别: 1.Array类型的变量在声明的同时必须进行实例化(至少得初花数组的大小),而ArrayList可以只是先声明: 2.Array始终是连续存放的:而ArrayL ...
- TypeToken获取运行时泛型类型
最近正好使用到了Guava的TypeToken来获取泛型的类型信息 比如,泛型父类需要获取其子类定义的泛型类型时: public abstract class GenericClazz<V> ...
- gulpfile.js备份
var gulp = require('gulp'); var uglify = require('gulp-uglify'); // var rename = require('gulp-renam ...
- one day php. alomost all;
<? namespace Test; use \PhpProject\PhpApp as Other; $u=new Other("ns test"); echo $u-&g ...
- Atcoder CODE FESTIVAL 2017 qual B D - 101 to 010 dp
题目链接 题意 对于一个\(01\)串,如果其中存在子串\(101\),则可以将它变成\(010\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...
- phpcms V9 广告模块中广告模板修改
广告模块模板位置 \phpcms\modules\poster\install\templates\*.html 我的需求: 去掉边框控制代码,是否显示边框我将在页面模板中设置,因些需要删除模板中的以 ...
- Scanner用法
先来看一个简单的例子: import java.util.*; public class ScannerTest { public static void main(String[] args){ ...