Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中读取文件_Android_File_Android File - Fatal编程技术网

在android中读取文件

在android中读取文件,android,file,android-file,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, bu

在以下代码中:

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
正在正确计算文件的大小。
你能告诉我哪里出了问题吗?

试试这个

 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;
}

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