How do you count the data from the file to the data model?



  • The data structure shall be repeated in the file, for example:

    name1
    name2
    name3
    .....
    name10
    

    name1
    name2
    name3
    .....
    name10

    Data should be considered as a model. I read:

    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 that's not what I want. What's the mistake?



  • I think the next line is extra.

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

    Your file is counting fast. And each. line appropriate name

    Try doing 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 I don't understand the meaning of the last team.

    I suspect my decision isn't exactly in point, but if you give more information, class. myApplicationDataI' help you more accurately.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2