Android的读取大文件 [英] Android reading a large file

查看:452
本文介绍了Android的读取大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在资产 CSV文件夹拥有超过10000行数据。我希望在创建数据库时这个数据插入到一个SQLite数据库。我不能包括一个数据库,因为它是一个非常通用应用程序和模型要求的CSV文件来代替。我不想读取所有10000条数据线以及存储在一个舒展插入。我该如何有效地完成任务?

I have a CSV file in the assets folder with more than 10000 lines of data. I want to insert this data into a SQLite database when the database is created. I cannot include a database because it is a very generic application and the model demands a CSV file instead. I don't want to read all 10000 lines of data and insert it from the memory in one stretch. How do I accomplish the task effectively and efficiently?

推荐答案

只要插入的立即的,一旦你读过的行。所以,只要不某些ArrayList中的行存储在内存中。

Just insert immediately once you've read the line. So, just don't store the lines in some arraylist in memory.

例如

while ((line = reader.readLine()) != null) {
    insert(line);
}

您可能会想这样做在一个事务中,这样每当中途失败,您可以回滚。在JDBC的一面,你可能要考虑 $ P $#pparedStatement addBatch() / #executeBatch()来分批执行该插入。一个例子可以在<一个中找到href=\"http://stackoverflow.com/questions/4355046/java-insert-multiple-rows-into-mysql-with-$p$pparedstatement/4355097#4355097\">this回答。

You might want to do this in a single transaction so that you can rollback whenever it fails halfway. In the JDBC side, you may want to consider PreparedStatement#addBatch()/#executeBatch() to execute the inserts in batches. An example can be found in this answer.

更新:作为一个完全不同的,但更有效的替代方案,你也可以离开Java的故事之外,它的 CSV导入工具通过SQLite的提供。

Update: as a completely different but more efficient alternative, you can also leave Java outside the story and use CSV import facility provided by SQLite.

这篇关于Android的读取大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆