我注意到在读取大文件(在我的例子中是csv文件)时使用java.util.Scanner非常慢。
我想改变当前读取文件的方式,以提高性能。下面是我目前所拥有的。请注意,我正在为Android开发:InputStreamReader inputStreamReader;
try {
inputStreamReader = new InputStreamReader(context.getAssets().open("MyFile.csv"));
Scanner inputStream = new Scanner(inputStreamReader);
inputStream.nextLine(); // Ignores the first line
while (inputStream.hasNext()) {
String data = inputStream.nextLine(); // Gets a whole line
String[] line = data.split(","); // Splits the line up into a string array
if (line.length > 1) {
// Do stuff, e.g:
String value = line[1];
}
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
使用Traceview,我发现主要的性能问题,特别是:java.util.Scanner.nextLine()和j