android eclipse 將項目中assets文件夾下的數據庫文件存儲到data/data/包名/databases下


android eclipse 將項目中assets文件夾下的數據庫文件存儲到data/data/包名/databases下:

public SQLiteDatabase openDatabase()
{
String pathFile = "data/data/com.example/databases/clearpath.db";//數據庫存儲路徑;
String pathMdkir = "data/data/com.example/databases";//數據庫存儲文件夾;
File path = new File(pathFile);
//判斷文件是否已經生成,如果存在就直接返回數據;
if(path.exists())
{
SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(path, null);
return db;
}
else
{
File path1 = new File(pathMdkir);
if(path1.mkdir())
{
System.out.println("創建成功");
}
else
{
System.out.println("創建失敗");
}
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(context.getAssets().open("clearpath.db"));
bos = new BufferedOutputStream(new FileOutputStream(pathFile));
int len;
byte[] b = new byte[1024];
while((len = bis.read(b)) != -1)
{
bos.write(b, 0, len);
bos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(bis != null)
{
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null)
{
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return openDatabase();
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com