Read and split text file into an array - Android

前端 未结 4 809
说谎
说谎 2020-12-22 05:07

I have gone through loads of these questions but still cant seem to figure it out. I have a text file split into rows. Each row consists of 5 pieces of data separated by a \

4条回答
  •  攒了一身酷
    2020-12-22 05:30

    See Using BufferedReader.readLine() in a while loop properly for an example of reading a file line by line using a buffered reader, then combine this with Kon's answer and I think you have a solution.

    AssetManager manger;
    String line = null;
    List xyzList = new ArrayList();
    String[][] xyz;  
    InputStream is;
    InputStreamReader isr;
    BufferedReader br;
    try {
        manger = getAssets();
        is = manager.open("data.txt");
        isr = new InputStreamReader(is);
        br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
          xyzList.add(line.split(","));
        }
        xyz = (String[][])xyzList.toArray();
    }catch (IOException e1) {
        Toast.makeText(getBaseContext(), "Problem!", Toast.LENGTH_SHORT).show();
    }finally{
        br.close();
        isr.close();
        is.close();
    }
    

提交回复
热议问题