将资源文件夹中的文件通过流的方式写入到应用的File文件夹中

//1.在Files文件夹中创建同名的数据库文件
File files = getFilesDir();
File file = new File(files, DBName);
if(file.exists()){
//如果文件存在,则代码返回,不往下运行
return;
}
InputStream inputStream = null;
FileOutputStream fos = null;

try {
//2.用输入流读取assets文件夹下的文件
inputStream = getAssets().open(DBName);
//3.将读取到的文件通过输出流写入到指定文件夹的文件中去
fos = new FileOutputStream(file);
//4.每次读取1024个字节
byte[] bs = new byte[1024];
int temp = -1;

//读取
while((temp = inputStream.read(bs))!=-1){
fos.write(bs, 0, temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null && fos!=null){
try {
inputStream.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

原文地址:https://www.cnblogs.com/kim-liu/p/7401676.html