如何读取大型 Sqlite 文件并从资产文件夹复制到 Android 模拟器或设备中?
我想很多人已经读过这篇文章:
时,它不断带来 IOException
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
但是,在我尝试使用大型数据库文件 。 大小>8MB 我在 Mac OS X 中使用 sqlite3 构建它,插入 UTF-8 编码字符串(因为我使用韩语), 添加了 android_meta 表,其中 ko_KR 作为区域设置,如上所述。
但是,当我调试时,它不断显示 IOException,
length=myInput.read(buffer)
我怀疑这是由于尝试读取大文件引起的。如果没有,我不知道为什么。 我使用更小的文本文件测试了相同的代码,并且运行良好。
有人可以帮我解决这个问题吗?我搜索了很多地方,但没有一个地方给我明确的答案或好的解决方案。 好的意思是高效或简单。
我会尝试使用BufferedInput(Output)Stream,但如果更简单的不能工作,我认为这也不起作用。
谁能解释一下 Android 中文件输入/输出的基本限制,以及可能的解决方法吗? 我真的很感激任何人的体贴回答。谢谢。
更多细节:
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
I guess many people already read this article:
However it's keep bringing IOException at
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
I’am trying to use a large DB file. It’s as big as >8MB
I built it using sqlite3 in Mac OS X, inserted UTF-8 encoded strings (for I am using Korean),
added android_meta table with ko_KR as locale, as instructed above.
However, When I debug, it keeps showing IOException at
length=myInput.read(buffer)
I suspect it’s caused by trying to read a big file. If not, I have no clue why.
I tested the same code using much smaller text file, and it worked fine.
Can anyone help me out on this? I’ve searched many places, but no place gave me the clear answer, or good solution.
Good meaning efficient or easy.
I will try use BufferedInput(Output)Stream, but if the simpler one cannot work, I don’t think this will work either.
Can anyone explain the fundamental limits in file input/output in Android, and the right way around it, possibly?
I will really appreciate anyone’s considerate answer. Thank you.
WITH MORE DETAIL:
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用较小的文件。或者在解包时将一组较小的文件拼接成一个大文件。或者在第一次运行应用程序时下载数据库。
Use a smaller file. Or a set of smaller files that you stitch together into a large file as you are unpacking them. Or download the database on the first run of your application.
我去过那里。我相信 RAW 资源类型没有最大大小限制(而资产,或更具体地说,连接到资产的流的大小受到限制)。
一旦你克服了这个障碍,你应该就没问题了 - 我手机上的数据库已经超过 100MB。
I've been there. I believe the RAW resource type is free of maximum size constraints (whereas Assets, or more specifically, the stream connected to an asset, is limited in size).
Once you get past that hurdle you should be fine - I've had databases on my phone well over 100MB.
不知道它是否对您有帮助,但如果其他方法失败,您可以尝试使用 NIO。例如,这是一个实现 将输入流复制到文件输出。或者,您可以尝试压缩文件并查看 Android 解压缩是否成功工具可以直接解压到你的db文件夹中。
Don't know if it would help you, but if other approaches fail you could try using NIO. For example, here's an implementation of copying inputstream to fileoutput. Alternatively, you could try zipping your file and see if the Android unzipping tools can unzip directly to your db folder.