我已将数据库文件放入assets文件夹,并希望将其复制到/ data / data //数据库文件夹。为此,我编写了以下代码:
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();
}
当我调用copy()时,不会复制数据库文件。可能的原因是什么?我怎样才能解决这个问题 ??而是出现了祝酒词。这意味着我的程序获得异常?但为什么 ??
答案 0 :(得分:0)
我有这个代码用于复制:
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();
}
}