-1

我正在使用下面的代码将包含文件和文件夹及其子文件夹的数据复制到我的应用程序的数据目录,但我得到异常,告诉目标位置不存在但是FileOutputStream方法应该根据 javadoc 为该方法创建目标文件夹:

Constructs a new FileOutputStream that writes to path. The file will be truncated if it exists, and created if it doesn't exist.

代码是:

    private void copyFileOrDir(String path) {
    AssetManager assetManager = this.getAssets();
    String assets[] = null;
    try {
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            String fullPath = "/data/data/" + this.getPackageName() + "/" + path;
            File dir = new File(fullPath);
            if (!dir.exists())
                dir.mkdir();
            File innerDir;
            for (int i = 0; i < assets.length; ++i) {
                copyFileOrDir(path + "/" + assets[i]);
            }
        }
    } catch (IOException ex) {
        Log.e("tag", "I/O Exception", ex);
    }
}

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
                    //getting exception here...
        Log.e("mgh", e.getMessage());
    }

}

对于任何文件夹,logcat 错误如下所示:

09-06 15:14:20.981: E/mgh(19262): /data/data/ir.example.sampleapplication/pzl/ui/css/main.css: open failed: ENOENT (No such file or directory)
4

1 回答 1

0

/*我正在使用此代码从 assets 文件夹中复制数据,试一试 */ public class DataBaseHelper extends SQLiteOpenHelper{

private static String DB_PATH = "/data/data/com.astrobix.numerodaily/databases/";
private static String DB_NAME = "Astrobix";
private static final String tag = "DatabaseHelperClass";
public static SQLiteDatabase myDataBase; 
public final Context myContext;
public static String Table_Name="Prediction";
public static final String COL_ID="ID";
public static final String COL_HEAD="HEAD";
public static final String COL_VALUE="VALUE";
private static final String TABLE_QUESTIONS ="Prediction";

private static final String Create_Table="create table if not exists "
                                         +Table_Name
                                         +"("
                                         +COL_ID
                                         +" INTEGER primary key autoincrement , "
                                         +COL_HEAD
                                         +" TEXT, "
                                         +COL_VALUE
                                         + " VARCHAR); ";

// 公共静态字符串 lastrecord; // public static String ratio="7:7";

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){        
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();     
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        //database does't exist yet.
    }
    if(checkDB != null){
        checkDB.close();
    }
    return checkDB != null ? true : false;

    /* File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();*/
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open("Astrobix.sqlite");

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE );     
}

@Override
public synchronized void close() {

    if(myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(Create_Table);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (db != null)
        onCreate(db);

}
于 2013-09-06T10:53:50.487 回答