Every 10 lines in the file, the data structure is repeated, for example:

name1 name2 name3 ..... name10 name1 name2 name3 ..... name10 

It is necessary to read the data in the model. I read like this:

 File sdcard = Environment.getExternalStorageDirectory(); File file = new File(sdcard,"file.txt"); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { Data data= new Data(); String[] strings = TextUtils.split(line, " "); for(int i=0;i<11;i++){ myApplication.setDataValue1(strings[0].trim); myApplication.setDataValue2(strings[1].trim); myApplication.setDataValue3(strings[2].trim); ... } data.setData(myApplication.getDataValue); } br.close(); } catch (IOException e) { } 

But in this way it is not at all read what I would like. Where is the mistake?

  • If you have 10 name elements, then the cycle should be either up to 10 or from 1 to 11. But not from 0 to 11. - Yuriy SPb 6:26 pm
  • And what error gives? - iramm

3 answers 3

I think the next line is superfluous.

 String[] strings = TextUtils.split(line, " "); 

After all, your file is read line by line. And in each line will be a corresponding name .

Try to do something like:

 int j = 0; String [] strings = new String [11]; while ((line = br.readLine()) != null) { strings[j % 11] = line; j++; if (j % 11 == 0) { myApplication.setDataValue1(strings[0].trim); myApplication.setDataValue2(strings[1].trim); ... myApplication.setDataValue2(strings[9].trim); data.setData(myApplication.getDataValue()); } } 

Although the meaning of the last command is incomprehensible to me (getDataValue is that a method or a field).

I suspect that my decision is not exactly to the point, but if you provide more information - the class myApplication , Data , then you can help you more accurately.

  • getDataValue is a getter in the Data field, the Data class contains the following data: private String value1 private String value2 private String value3 .. class myApplication - an instance of the Data class is created in it, and a getter is created. - Sergey
  • @ Sergey It is not clear how the multiple data sets name1, ..., name10 are used. Are they assigned to the same myApplication instance? What returns myApplication.getDataValue (). Please cite the setDataValuei () methods in the getDataValue, setData () method. It is better to bring the whole classes, excluding only that which is completely irrelevant. - iramm
  • @ Sergey The answer is corrected - one empty line was taken into account between the blocks in the file (10 replaced by 11), but only the first 10 elements from the array are used. - iramm
  • Thanks, I used your idea, but I was wrong that I used the link to the already created link, and your version was very useful. - Sergey
  1. Try to read the entire file first in one line.

  2. Then cut the string into an array with a two-line delimiter \n\n

  3. It now remains to cut each cell in the array by delimiter into a single line break \n

    I will add my source:

     try { // Конструкция, авто закрытие файла try(FileInputStream file = new FileInputStream("test")){ // Если в файле байтов хотя бы 1 if(file.available() > 0){ // Буфер, первоначальное количество 10 байт ByteArrayOutputStream out = new ByteArrayOutputStream(10); int a = -1; while(true){ a = file.read(); // Читаем поток // -1 конец (всё, в файле ничего нету) if(a == -1) break; // 10 это \n if(a == 10){ // Пишу считанную строку, что с ней еще делать... System.out.println(out.toString()); out.reset(); // Чистим буфер continue; } out.write(a); // Пишем считанный байт в буфер } } } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } 

    PS (Buffered)Reader I do not advise: it consumes a lot of memory (and if the file is huge, then even more). If necessary, I can do trim on the fly without using trim ().