从android中的文件读取

时间:2011-09-30 01:17:39

标签: android file android-file

在以下代码中:

fileInputStream = new FileInputStream(new File(pathToOurFile) );
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

打印缓冲区提供一些随机值,而不是文件中的内容。 buffersize正确计算文件的大小 你能告诉wat出错吗?

1 个答案:

答案 0 :(得分:1)

试试这个..

 private void ReadFile(AssetManager manager, String sourceFileName,
        String destinationFileName) throws IOException {

    // Read file from AccessManager
    InputStream inputStream = manager.open(sourceFileName);
    OutputStream outputStream = new FileOutputStream(destinationFileName);
    Log.d("-->", "src: " + sourceFileName);
    Log.d("-->", "Des: " + destinationFileName);
    byte[] buffer = new byte[3072];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {

        outputStream.write(buffer, 0, length);

    }

    outputStream.flush();
    outputStream.close();
    inputStream.close();

    outputStream = null;
    inputStream = null;
}