如果你想分块读取那个巨大的文件,你可以这样做AsyncTask
:
static class StreamTask extends AsyncTask<String, Void, Integer> {
private static final int BUFFER_LENGTH = 1024 * 8; // Adjust to taste
// Param #0 = file name
// Param #1 = charset name
@Override
protected Integer doInBackground(String... params) {
if (params.length != 2) {
throw new IllegalArgumentException();
}
int chars = 0;
CharsetDecoder cd = Charset.forName(params[1]).newDecoder();
try {
FileInputStream fin = new FileInputStream(params[0]);
try {
FileChannel fc = fin.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(BUFFER_LENGTH);
CharBuffer cb = CharBuffer.allocate(BUFFER_LENGTH);
while (fc.read(bb) != -1) {
// Flip the buffer, decode the contents
bb.flip();
cd.decode(bb, cb, false); // You should probably look at CoderResult also.
// Flip & extract the decoded characters.
cb.flip();
chars += cb.remaining();
onCharacters(cb.array(), cb.position(), cb.remaining());
cb.clear();
// Prepare the buffer for reuse.
bb.compact();
}
// fc.read(..) returned -1 -> EOF, but bb may still contain
// stuff to decode.
bb.flip();
cd.decode(bb, cb, true);
cd.flush(cb);
cb.flip();
if (cb.remaining() > 0) {
chars += cb.remaining();
onCharacters(cb.array(), cb.position(), cb.remaining());
}
} finally {
fin.close();
}
} catch (IOException e) {
chars = -1;
}
return chars;
}
protected void onCharacters(char[] ch, int offset, int length) {
// Do something with the characters (still running in the AsyncTask thread)
}
}