Java Android无法将字节数据复制到空文件
你好,我正在构建一个使用12Mb数据库的小应用程序。 由于db的大小,我必须从资产文件夹复制它,我使用字节数组将数据复制到一个空文件:Java Android无法将字节数据复制到空文件,java,android,Java,Android,你好,我正在构建一个使用12Mb数据库的小应用程序。 由于db的大小,我必须从资产文件夹复制它,我使用字节数组将数据复制到一个空文件: @Override public void onCreate(SQLiteDatabase db){ try{ //Getting the asset db InputStream myInput = this.context.getAssets().open("dicoMots.db"); //Creating
@Override
public void onCreate(SQLiteDatabase db){
try{
//Getting the asset db
InputStream myInput = this.context.getAssets().open("dicoMots.db");
//Creating the empty db to receive bytes
String outFileName = "/data/data/"+this.context.getApplicationContext().getPackageName()+"/databases/dicoMots.db";
OutputStream myOutput = new FileOutputStream(outFileName);
//Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[14336000];
int length;
while ((length = myInput.read(buffer)) > 0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
Log.d("DICO", "Database loaded");
}
catch(Exception e){
Log.d("DICO", "Database load fail");
}
}
成功创建空文件,并读取输入文件。myInput.read返回的值也是正确的。
但是输出文件仍然为空,并且没有复制任何数据。此方法对我有效
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_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();
}