Android的文件拷贝 [英] Android file copy

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

问题描述

我发现读一行从SD卡上的文本文件中的时间是相当缓慢的。我想,这可能会更快,如果该文件在内存中,所以我想从SD卡内部存储复制文件。

I am finding that reading one line at a time from a text file on the SD card is rather slow. I imagine that it might be quicker if the file is in internal memory, so I want to copy files from the SD card to internal storage.

文件复制的例子我能找到在网络上似乎涉及从一个InputStream复制一次一个字节一个OutputStream或从的FileReader到一个FileWriter。这真的是最快和最有效的方法?

The file copy examples I can find on the web seem to involve copying one byte at a time from an InputStream to an OutputStream or from a FileReader to a FileWriter. Is this really the quickest and most efficient method?

推荐答案

如果你是拉在文件中使用你的应用程序是什么,我建议你做的是读取数据,那么东西你已经收集到了一些内存中的数据这类读者(的BufferedReader也许),这样你就可以从那里读取行。

If you are pulling the file in for use in your application what I suggest you do is read in the data then stuff the in memory data you have collected into some kind of reader (BufferedReader perhaps) so that you can then read the lines from there.

下面是什么,我通常做的一个例子:

Here is an example of what I typically do:

// Assumption: I already have the file object I want to read
// Note: I'm not doing any error handling.
InputStream input = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while( (bytesRead = input.read(buffer)) > 0){
    baos.write(buffer, 0, bytesRead);
}
StringReader stringReader = new StringReader( new String(baos.toByteArray()) );
BufferedReader bufferedReader = new BufferedReader( stringReader );
for(String line : bufferedReader.readLine()){
    // TODO: Handle each line appropriately or something
    Log.d("Reading Data Example", line);
}

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

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