FileInputStream读取文件一直返回0

FileInputStream read file keep returning 0

我正在使用 FileInputStream 读取 android phone 上的文件。但是当我使用 FileInputStream.read 将数据读入数组时, return 值始终为 0。我检查了路径和文件名是否正确。可能的原因是什么?

in = new FileInputStream(inFilename)
int readsize = 0;//Read size keep returning 0. 
do{
    readsize = in.read(data);
    Log.d(Constants.TAG, "Readsize:"+readsize);
    out.write(data);
} while(readsize > 0 );

这只有在您提供零长度缓冲区或在三参数读取的情况下提供零计数时才有可能。

注意写行应该是

out.write(data, 0, readsize);

循环应该写成

while ((readsize = in.read(data)) > 0)
{
    out.write(data, 0, readsize);
}

按照您的方式,您在流结束时调用 write(),这是不正确的。

您也不需要初始化 readsize