private SQLiteDatabase openDatabase(String dbfile) { try { if (!(new File(dbfile).exists())) { //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库 File f = new File(DB_PATH); // 如 database 目录不存在,新建该目录 if (!f.exists()) { f.mkdir(); } try { InputStream is = this.context.getResources().openRawResource( R.raw.onedic); // 输出流 OutputStream os = new FileOutputStream(dbfile); // 文件写入 byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } // 关闭文件流 os.flush(); os.close(); is.close(); }catch (Exception e){ e.printStackTrace(); } } SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); return db; } catch (Exception e) { Log.e("Database", "File not found"); e.printStackTrace(); } return null; } 这个是我把在assets文件下的数据库写入手机里面的部分代码 然后就报错: ![图片描述][1]
为什么不直接读,而要先进行一下文件读写操作呢