Whole text file to a String in Java

后端 未结 10 2269
遇见更好的自我
遇见更好的自我 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:17

    apache commons-io has:

    String str = FileUtils.readFileToString(file, "utf-8");
    

    But there is no such utility in the standard java classes. If you (for some reason) don't want external libraries, you'd have to reimplement it. Here are some examples, and alternatively, you can see how it is implemented by commons-io or Guava.

提交回复
热议问题