This example is applicable to Android. Maybe someone will be interested in solving my problem. I decided that I would not save the data of the entire file in the Map
, but I would read the file in a separate stream, as soon as the required value was encountered, the stream would return to us the value from the second column. I implemented all this in a separate AsyncTask
class. This code tested, works quickly and without glitches, the number of lines is about 2000 thousand.
Created the FindKeyAsyncTask
class FindKeyAsyncTask
This thread takes a String
- the value it is looking for, and returns a String
- the result.
public class FindKeyAsyncTask extends AsyncTask<String, Void, String> { Context context; public FindKeyAsyncTask(Context context) { super(); this.context = context; } @Override protected String doInBackground(String... params) { final String FILENAME = "data.db"; String result = ""; String str = ""; try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(context.openFileInput(FILENAME))); while ((str = bufferedReader.readLine()) != null) { String[] array = str.split("\t"); if (array[0].equals(params[0])){ result=array[1]; return result; }else { result = context.getString(R.string.f2_NotFound); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); FindFragment.tv_amount.setText(result); }
}