【问题标题】:Reading from a file in android从android中的文件读取
【发布时间】:2011-11-28 03:15:12
【问题描述】:

在以下代码中:

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 出了什么问题吗?

【问题讨论】:

  • 可能是你读错了文件。请检查文件位置及其内容。
  • 请显示其余代码、maxBufferSize 以及实际打印缓冲区的方式。
  • 其余代码都很好,现在我尝试了另一种方法,它正在工作。

标签: android file android-file


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    相关资源
    最近更新 更多