0

这是我正在使用的代码(在许多答案中都可以找到):

InputStream myInput;
        try {
            myInput = iNezamApplication.getAppContext().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();

        } catch (IOException e1) {
            e1.printStackTrace();

        }

但是,到达 OutpoutStream 行后,我总是遇到异常:

java.io.FileNotFoundException: /data/data/package_name/databases/databasename.db: open failed: ENOENT (No such file or directory)
4

3 回答 3

6

我试过这样的东西..

final String[] sample_dbName = {"DrugsNew.db"};

    int assetDbSize = sample_dbName.length;
    File databaseFile = new File( "/data/data/com.handyrep2.ui/databases");

    // check if databases folder exists, if not create one and its subfolders
    if (!databaseFile.exists()){
        databaseFile.mkdir();
    }

        for(int i=0;i<assetDbSize;i++){
            String outFilename =null;

        outFilename = "/data/data/com.handyrep2.ui/databases"+ "/" + sample_dbName[i];

            File sampleFile = new File(outFilename);

                try {
                    InputStream in = activity.getAssets().open("offlinedb/"+sample_dbName[i]);

                    OutputStream out = new FileOutputStream(outFilename);
                    // Transfer bytes from the sample input file to the sample output file
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                      out.write(buf, 0, len);
                    }
                    out.flush();
                    // Close the streams
                    out.close();
                    in.close();
                }catch(IOException e) {

                }


        }
于 2013-07-10T12:24:37.803 回答
1

“package_name”是你真正的包名的替代品,只是为了把它贴在这里,还是真的在你的 DB_PATH 中使用它?:)

于 2013-07-10T12:12:09.480 回答
1

您必须首先创建一个 File 对象

于 2013-07-10T12:16:01.100 回答