There is a DBHelper helper class inherited from SQLiteOpenHelper , which has all the methods for working with a database (opening, creating, updating, etc.). In the assets folder there is a plain text db.txt .

Is it DBHelper to access the db.txt file in the db.txt ? Through getAssets () it is impossible.

Wrote like this, but displays nothing

 public void getStringFromAssetFile(Context myContext) { AssetManager am = myContext.getAssets(); try { BufferedReader buf = new BufferedReader(new InputStreamReader(am.open("db.txt"))); String tmpStr; while ((tmpStr = buf.readLine()) != null) { try { System.out.println(tmpStr); } catch (Exception e) { } } } catch (IOException ex) { ex.printStackTrace(); } } 

    2 answers 2

    Try this

     protected String readFileInAssets(String name){ String text = name; byte[] buffer = null; InputStream is; try { is = getAssets().open(text); int size = is.available(); buffer = new byte[size]; is.read(buffer); is.close(); } catch (IOException e) { e.printStackTrace(); } String str_data = new String(buffer); return str_data; } TextView tv=(TextView)findViewById(R.id.tv); tv.setText(readFileInAssets("db.txt",getApplicationContext())); 
    • In the description of the problem I wrote as I try to do - Michael
    • SQLiteOpenHelper has no getAssets() method - pavlofff

    1) The database in Android is stored in bdName format. sqlite 2) First you need to create a database by copying data from assets to the root of the application itself.

      public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (dbExist) { //ничего не делать - база уже есть } else { //вызывая этот метод создаем пустую базу, позже она будет перезаписана this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } this.close(); } } private boolean checkDataBase() { SQLiteDatabase tempDB = null; try { String myPath = DATABASE_PATH + DATABASE_NAME; File file = new File(myPath); if (file.exists() && !file.isDirectory()) tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } catch (SQLiteException e) { Log.e("Сheck", e.getMessage()); } if (tempDB != null) tempDB.close(); return tempDB != null ? true : false; } public void copyDataBase() throws IOException { try { InputStream myInput = mContext.getAssets().open("databases/" + DATABASE_NAME); String outputFileName = DATABASE_PATH + DATABASE_NAME; OutputStream myOutput = new FileOutputStream(outputFileName); 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 (Exception e) { Log.e("СopyDatabase", e.getMessage()); } } 

    This is all written in your DBHelper class.