The question is that there is a file (normal .txt), in this file it looks like 10 lines and two columns in each column of the number. It is necessary to work with this file as something, and that is, read the data and output the data from the second column by the number from the first column. How to do this, use a database or sharedPreference or arrays? Tell me please. The type of file that should be considered is:

  • 000000001 0002
  • 000000002 09809
  • 000000003 0002
  • 000000004 09809, etc.

    2 answers 2

    Why complicate things like that?

    final File data = new File("file.txt"); LineNumberReader lnr = new LineNumberReader(new BufferedReader(new FileReader(data))); String line; while((line = lnr.readLine()) != null) { final String args[] = line.split("\t", -1); //parse params args[0], args[1] int id_1 = Integer.parseInt(args[0]); int id_2 = Integer.parseInt(args[1]); } 
    • And how do I tie them? That is, I enter in on the screen 000000003 the answer 0002. - Yurii
    • in what sense to tie? Do you want to derive a completely different argument for one argument that is not comparable in lane? If yes, then compile the collection Map <K, V> and enter the parameters args [1] according to the key. If not, then here is the working function of parsing the parameters for the lane file. - GenCloud
    • Maybe I do not correctly explain how, sorry, the point is that there is a file in it there is information. Suppose 00000003 and its value is 0002. I need to somehow save all this in an array or something else that I could then refer to the number 00000003 and get its value 0002. - Yurii
    • not a question, do the Map <K, V> where key is 00000003, and value is 0002, i.e. Map <Integer, Integer> data = new HashMap <> (); data.put (id_1, id_2); use as, data.get (key) <- returns the id_2 parameter. Learn java sir - GenCloud
    • Thank you, I will definitely deal with this. - Yurii

    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); } 

    }