把assets目錄下的db文件拷貝進來

 public void copyDb(String filename) {
        File file = new File(getFilesDir(), filename);
        if (file.exists() && file.length() > 0) {
            Log.i(TAG, "DB 已經存在不需要拷貝");
        } else {
            try {
                InputStream is = getAssets().open(filename);
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int len = -1;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章