简体   繁体   English

FileInputStream读取的文件不断返回0

[英]FileInputStream read file keep returning 0

I am using FileInputStream to read a file on android phone. 我正在使用FileInputStream在Android手机上读取文件。 But when I used the FileInputStream.read to read data into array, the return value is always 0. I have checked that the path and file name is correct. 但是,当我使用FileInputStream.read将数据读入数组时,返回值始终为0。我检查了路径和文件名是否正确。 Whats the possible reasons? 可能的原因是什么?

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

This is only possible if you supplied a zero-length buffer, or a zero count in the case of the three-argument read. 仅当您提供了零长度的缓冲区,或者在三参数读取的情况下提供了零计数时,才有可能这样做。

NB The write line should be 注意:写入行应为

out.write(data, 0, readsize);

and the loop should be written as 循环应该写成

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

The way you have it, you're calling write() at end of stream, which isn't correct. 拥有它的方式是,在流的末尾调用write() ,这是不正确的。

You don't need to initialize readsize either. 您也不需要初始化readsize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM