[英]Reading text-file in res/raw, very slow. Using BufferedReader
The size of the text-file is 560 kilobytes and has about 24500 rows. 文本文件的大小为560 KB,约有24500行。 Each row is added to a list.
每行添加到一个列表。 Maybe my phone is to old and slow?
也许我的手机又旧又慢? Phone-model: Samsung GT-S5570 with Android 2.3.4.
手机型号:Samsung GT-S5570和Android 2.3.4。 It takes about 30 seconds or more to read it and I'm pretty sure that my algorithms outside the Reader-class is not the problem.
读取它大约需要30秒或更长时间,而且我很确定我的Reader类之外的算法不是问题。 Anyone else who has encountered a similar problem or has an idea of what the problem could be?
其他任何遇到过类似问题或对问题可能有想法的人吗?
public class Reader {
public List<String> read(String file) {
Context ctx = ApplicationContextProvider.getContext();
List<String> entries = new ArrayList<String>();
//Get res/raw text-file id.
int resId = ctx.getResources().getIdentifier(file,"raw", ctx.getPackageName());
InputStream inputStream = ctx.getResources().openRawResource(resId);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 8192);
try {
String test;
while (true) {
test = reader.readLine();
if (test == null)
break;
entries.add(test);
}
inputStream.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return entries;
}
}
The device you describe is a very old and slow device. 您描述的设备是非常老旧且缓慢的设备。 You should take care to speedup the reading process as much as possible by:
您应该注意通过以下方式尽可能地加快阅读过程:
It is also a good idea to measure how much time each part takes - reading the file and inserting the 24500 strings into the ArrayList. 衡量每个部分需要多少时间也是一个好主意-读取文件并将24500字符串插入ArrayList。 Bad performance may come from the least expected direction.
效果不佳可能来自最不期望的方向。
Please try the following approach and share the results (if possible with time measurements): 请尝试以下方法并共享结果(如果可能的话,还可以进行时间测量):
private char[] readWholeFile(String file) {
Context ctx = ApplicationContextProvider.getContext();
int resId = ctx.getResources().getIdentifier(file, "raw", ctx.getPackageName());
InputStream inputStream = ctx.getResources().openRawResource(resId);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 8192);
try {
int length = inputStream.available();
char[] contents = new char[length];
reader.read(contents, 0, length);
return contents;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (reader != null) reader.close();
} catch (Exception e) {
}
}
}
public List<String> readEntries() {
final int EXPECTED_ELEMENTS = 24500;
char[] contents = readWholeFile("somefile");
if (contents == null) {
return new ArrayList<String>();
}
List<String> entries = new ArrayList<String>(EXPECTED_ELEMENTS);
String test;
BufferedReader reader = new BufferedReader(new CharArrayReader(contents));
try {
while ((test = reader.readLine()) != null) {
entries.add(test);
}
return entries;
} catch (IOException e) {
e.printStackTrace();
return new ArrayList<String>();
} finally {
try {
if (reader != null) reader.close();
} catch (Exception e) {
}
}
}
If you use IOUtils from apache "commons-io" it's even easier. 如果您使用apache“ commons-io”中的IOUtils,则更加简单。
InputStream is = getResources().openRawResource(R.raw.yourNewTextFile);
String s = IOUtils.toString(is);
IOUtils.closeQuietly(is); // don't forget to close your streams
you can download them from http://commons.apache.org/proper/commons-io/ or http://mvnrepository.com/artifact/commons-io/commons-io 您可以从http://commons.apache.org/proper/commons-io/或http://mvnrepository.com/artifact/commons-io/commons-io下载它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.