[英]How to copy database file from asset folder to /data/data/<package-name>/database folder
I have put a database file in to assets folder and want to copy it to /data/data//database folder . 我已将数据库文件放入资产文件夹,并希望将其复制到/ data / data // database文件夹。 For this purpose , I have written the following code :
为此,我编写了以下代码:
public void copy()
{
try
{
String dest_path = "/data/data/"+getPackageName()+"/database/sultandatabase";
File f = new File(dest_path);
if( !f.exists() )
{
copy_database(getBaseContext().getAssets().open(db_name),new FileOutputStream(dest_path));
}
}
catch(Exception e)
{
Toast.makeText(this,"Sorry the file can not be opended", Toast.LENGTH_LONG).show();
}
}
public void copy_database(InputStream io ,OutputStream ou) throws Exception
{
byte[] buffer = new byte[1024] ;
int lenght;
while( ( lenght = io.read(buffer)) > 0 )
ou.write(buffer);
io.close();
ou.close();
}
When I call copy() the database file is not copied . 当我调用copy()时,数据库文件未复制。 What is the possible reason ??
可能是什么原因? How can I fix this ??
我怎样才能解决这个问题 ?? Instead a toast is shown up .
取而代之的是敬酒。 That means my program gets Exception ??
这意味着我的程序得到了异常? But why ??
但为什么 ??
i have this code for copy: 我有此代码要复制:
private void copyDataBase()
{
try
{
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.