Fastest/Cleanest way to load a text file in memory

南楼画角 提交于 2019-12-21 04:05:16

问题


I know similar questions have been asked before, but I couldn't find one that answers my exact question.

I need a way to read a file as a String with the least code and as simple and as optimal as possible.

I'm not looking for:

final BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {

  // logic
}

And I know I can write my own helper class that does this.

I'm looking for something more along the lines of:

final String wholeFileAsStr = Something.load(file);

Where Something.load() is super optimized and buffers the file properly while reading it, taking file size into account for instance.

Can anyone recommend something from Guava or Apache maybe that I'm not aware of ?

Thanks in advance.


回答1:


Perhaps IOUtils.toString , from Commons IOUtils




回答2:


For a they detailed look at all various methods of reading a single file in a JVM try the following article:

Java tip: How to read files quickly



来源:https://stackoverflow.com/questions/5432852/fastest-cleanest-way-to-load-a-text-file-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!