源代码如下所示
public static ArrayList<Integer> readFile(String fileName) {
String sdPath;
sdPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
ArrayList<Integer> data = new ArrayList<Integer>();
try {
Scanner s = new Scanner(new FileInputStream(new File(sdPath + fileName)));
while( s.hasNext() ) {
data.add( s.nextInt() );
}
}
catch(FileNotFoundException fN) {
fN.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
}
return data;
}
当我运行此代码时,LogCat会在我的android
中显示以下消息并显示任何更改.
.
.
04-27 00:12:31.216: D/dalvikvm(17141): GC_CONCURRENT freed 1953K, 23% free 15699K/20295K, paused 2ms+12ms
.
.
.
此外,上面的代码可以在几秒钟内在我的桌面上完全运行..
请帮助我。
答案 0 :(得分:3)
与台式电脑相比,手机的资源非常有限。您不应期望手机能够以与台式计算机相同的速度完成计算密集型任务......
你正在耗尽所有试图将大量ArrayList加载到其中的手机RAM。
一些提示:
答案 1 :(得分:0)
只需将数据保存在磁盘上,然后使用内存映射IO。看看FileChannel and the map function然后从那里拿走它。缓冲类也有methods to retrieve integers。