Whole text file to a String in Java

后端 未结 10 2264
遇见更好的自我
遇见更好的自我 2020-12-01 15:44

Does Java has a one line instruction to read to a text file, like what C# has?

I mean, is there something equivalent to this in Java?:

String data =          


        
10条回答
  •  清歌不尽
    2020-12-01 16:02

    Not within the main Java libraries, but you can use Guava:

    String data = Files.asCharSource(new File("path.txt"), Charsets.UTF_8).read();
    

    Or to read lines:

    List lines = Files.readLines( new File("path.txt"), Charsets.UTF_8 );
    

    Of course I'm sure there are other 3rd party libraries which would make it similarly easy - I'm just most familiar with Guava.

提交回复
热议问题