获取文件的byte数组与根据byte数组生成文件的工具类示例
2017-11-28 16:06
681 查看
public class ByteToFile {
//获得指定文件的byte数组
public static byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
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;
}
//根据byte数组,生成文件
public static File 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.mkdirs();
}
file = new File(filePath+File.separator+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
bos.flush();
} 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();
}
}
}
return file;
}
}
//获得指定文件的byte数组
public static byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
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;
}
//根据byte数组,生成文件
public static File 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.mkdirs();
}
file = new File(filePath+File.separator+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
bos.flush();
} 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();
}
}
}
return file;
}
}
相关文章推荐
- Android中根据byte数组生成文件并保存到手机
- Android中根据byte数组生成文件并保存到手机
- Android Studio apk打包,keystore.jks文件生成,根据keystore密钥获取SHA1安全码
- ruby获取本机mac加密生成lisence文件,客户端根据lisence许可调用主程序
- FreeMarker工具类,用来根据模板文件生成html文件,html字符串等
- Android中byte数组生成文件并保存到手机
- Android Eclipse keystore.jks文件生成,根据keystore密钥获取SHA1安全码 ,apk打包
- android 字符串工具类 格式化日期字符串 拼接数组 判断字符串是否为空 转换时间显示 转换文件大小 获取中英文字符个数
- java获取配置文件及根据key获取配置文件中的value的工具类
- 根据取到文件的内容 生成二进制文件 并通过servlet获取
- IOS工具类 (钱转化(分到元),快速创建控件,生成图片名称,验证信息,缓存文件,友盟统计类,转换为json,获取URl中的参数)
- 获取文件数据流+叠加byte数组(给byte数组加包头包尾)
- 根据Android Layout XM文件自动生成自定义View代码的工具类
- 获取文件字节数组byte[]
- Android Eclipse keystore.jks文件生成,根据keystore密钥获取SHA1安全码 ,apk打包
- PHP实现的获取文件mimes类型工具类示例
- 从服务器返回的字符串,先转成byte[],然后保存到文件,listview根据uri获取显示
- Android Studio apk打包,keystore.jks文件生成,根据keystore密钥获取SHA1安全码
- JAVA文件工具类之——文件写入(byte数组、String方式、url写入方式)