Android获取assets文件夹中的数据并写入SD卡示例
本文示例主要实现了Android获取assets文件夹中的数据并将其写入到SD卡中,该程序实现的步骤主要为:首先读取assets文件夹中的数据库,再将其写入到SD存储卡中。
完整示例代码如下:
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importandroid.content.Context;
/*将assets文件夹下的数据库写入SD卡中
*@authorDave*/
publicclassWriteToSD{
privateContextcontext;
StringfilePath=android.os.Environment.getExternalStorageDirectory()+"/weather";
publicWriteToSD(Contextcontext){
this.context=context;
if(!isExist()){
write();
}
}
privatevoidwrite(){
InputStreaminputStream;
try{
inputStream=context.getResources().getAssets().open("addressId.db");
Filefile=newFile(filePath);
if(!file.exists()){
file.mkdirs();
}
FileOutputStreamfileOutputStream=newFileOutputStream(filePath+"/database.db");
byte[]buffer=newbyte[512];
intcount=0;
while((count=inputStream.read(buffer))>0){
fileOutputStream.write(buffer,0,count);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
System.out.println("success");
}catch(IOExceptione){
e.printStackTrace();
}
}
privatebooleanisExist(){
Filefile=newFile(filePath+"/database.db");
if(file.exists()){
returntrue;
}else{
returnfalse;
}
}
}热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短