File和byte[]转换

来源:互联网 发布:强矩阵式管理 编辑:程序博客网 时间:2024/06/07 02:38
public static byte[] FileToByte(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 FileToByte(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();
}
}
}
}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 真中华一番 歌舞伎町一番街 中华一番第二部国语版 别是一番滋味在心头 一番街的奇迹 一番街奇迹 别有一番滋味在心头 中华一番极 真中华一番上映时间 中华一番粤语 歌舞伎町一番街旅游 一个番一个耳朵旁 一番寿司 一番寿司团购 一番是什么意思 番100 张生与崔莺莺一番云雨 一盏燕窝分几次吃合适 一盏春光 我是一盏路灯作文 一盏青灯伴古佛图片 一盏夜灯作品 一盏茶 盏组词 春夏秋冬一盏夜灯 假如我是一盏路灯 一盏茶时 燕窝一盏多少克 点一盏心灯 六安一盏作品 一盏 我是一盏路灯 盏拼音 我是一盏路灯500字 假如我是一盏路灯作文 一盏燕窝吃几次 一盏夜灯 盏的拼音 建盏开盏正确方法 建盏是什么