Reading a text file in java

前端 未结 7 1068
遥遥无期
遥遥无期 2020-11-30 11:50

How would I read a .txt file in Java and put every line in an array when every lines contains integers, strings, and doubles? And every line has different amounts of words/n

7条回答
  •  醉话见心
    2020-11-30 12:36

    This is a nice way to work with Streams and Collectors.

    List myList;
    try(BufferedReader reader = new BufferedReader(new FileReader("yourpath"))){
        myList = reader.lines() // This will return a Stream
                     .collect(Collectors.toList());
    }catch(Exception e){
        e.printStackTrace();
    }
    

    When working with Streams you have also multiple methods to filter, manipulate or reduce your input.

提交回复
热议问题