在开发中经常会使用到保存文件到SD里面。
//参数一、文件的byte流
//参数二、文件要保存的路径
//参数三、文件保存的名字
public static void saveFile(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, fileName);
if(file.exists()){
file.delete();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
//把需要保存的文件保存到SD卡中
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();
}
}
}
}
这是在项目开发中经常会用到的方法之一,再次记录。