Read text file into an array

后端 未结 5 893
遥遥无期
遥遥无期 2020-12-10 03:40

I want read a text file into an array. How can I do that?

data = new String[lines.size]

I don\'t want to hard code 10 in the array.

<         


        
5条回答
  •  庸人自扰
    2020-12-10 04:41

    Use a List instead. In the end, if you want, you can convert it back to an String[].

    BufferedReader abc = new BufferedReader(new FileReader(myfile));
    List data = new ArrayList();
    String s;
    while((s=abc.readLine())!=null) {
        data.add(s);
        System.out.println(s);
    }
    abc.close();
    

提交回复
热议问题