我一直在互联网上四处寻找,试图找出哪种方法可以读取不长的文本文件(这里的用例涉及小的OpenGL着色器)。我最终得到了这个:
private static String load(final String path)
{
String text = null;
try
{
final FileReader fileReader = new FileReader(path);
fileReader.read(CharBuffer.wrap(text));
// ...
}
catch(IOException e)
{
e.printStackTrace();
}
return text;
}
在哪些情况下,这段代码会导致效率低下?这是CharBuffer.wrap(text)
好事吗?
答案 0 :(得分:1)
我通常会像这样滚动。 CharBuffer.wrap(text)似乎只能让你获得一个角色... File Reader docs
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.3/Modules/FindBoost.cmake:1245 (message):
Unable to find the requested Boost libraries.
Boost version: 1.60.0
Boost include path: C:/Boost/boost_1_60_0
Could not find the following Boost libraries:
boost_system
boost_thread
boost_filesystem
boost_date_time
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the
directory containing Boost libraries or BOOST_ROOT to the location of
Boost.
Call Stack (most recent call first):
CMakeLists.txt:11 (find_package)
CMake Error at CMakeLists.txt:107 (set):
Syntax error in cmake code at
C:/libs/caffe/caffe-windows-dependencies/CMakeLists.txt:107
when parsing string
${CMAKE_INSTALL_
syntax error, unexpected $end, expecting } (16)
答案 1 :(得分:1)
如果您想逐行阅读文件:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
如果您想一次性阅读完整的文件:
String text = new String(Files.readAllBytes(...))或Files.readAllLines(...)