安卓文件拷贝

Android file copy(安卓文件拷贝)

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

问题描述

我发现从 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);
}

这篇关于安卓文件拷贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:安卓文件拷贝

基础教程推荐