【问题标题】:-1 detected while reading file java读取文件 java 时检测到 -1
【发布时间】:2017-10-25 19:00:01
【问题描述】:

java 中的服务器程序读取时,我得到了模棱两可的输出:

byte [] mybytearray  = new byte [content_length]; 
InputStream inputStream = clientSocket0.getInputStream(); //Getting input 
from peer0
FileOutputStream fileOutputStream = new FileOutputStream(fileDownloaded); 
//Sending the output to local directory
BufferedOutputStream bufferedOutputStream = new 
BufferedOutputStream(fileOutputStream);

bytesRead = inputStream.read(mybytearray,0,content_length);
System.out.println("First read : " +bytesRead);
current = bytesRead;
if(bytesRead!=content_length) {
              current = bytesRead;
               do {
                        System.out.println(current +"-Current");
                        System.out.println("Read it : "+(mybytearray.length-current));
                        bytesRead =
                              inputStream.read(mybytearray, current, (mybytearray.length-current));
                          System.out.println("***"+bytesRead);
                         if(bytesRead == -1) {
                             //current = content_length;
                             break;
                         }
                         else 
                           current += bytesRead;
                     } while(current < content_length );
                }

                bufferedOutputStream.write(mybytearray, 0 , current);

                bufferedOutputStream.flush();
                bufferedOutputStream.close();
                inputStream.close();
                inFromServer0.close();

它为某些文件提供以下输出:

内容长度 33996
C:\Users\Sumit\git\IP_Task2\Task1\Peer1/rfc8183.txt.pdf
初读:24356
24356-电流
阅读:9640
***-1

循环中的 bytesRead 为 -1,因此无法创建正确的文件。

【问题讨论】:

  • 请正确格式化您的代码!如果您使用的是最新版本的 Java,请考虑使用 try-with-resources 而不是手动调用 InputStream.close()

标签: java file byte


【解决方案1】:

每当你使用一个方法时,你应该阅读它的文档,看看它可以返回什么值。

看看InputStream.read(byte[], int, int)的描述:

公共 int 读取(字节 [] b, 关闭, 国际化) 抛出 IOException

从输入流中读取最多 len 个字节的数据到一个字节数组中。尝试读取多达 len 个字节,但可能会读取较小的字节数。实际读取的字节数以整数形式返回。

在输入数据可用、检测到文件结尾或引发异常之前,此方法会一直阻塞。

如果 len 为零,则不读取任何字节并返回 0;否则,将尝试读取至少一个字节。如果由于流位于文件末尾而没有可用字节,则返回值 -1;否则,至少读取一个字节并将其存储到 b 中。

读取的第一个字节存储在元素 b[off] 中,下一个字节存储在 b[off+1] 中,依此类推。读取的字节数最多等于 len。设 k 为实际读取的字节数;这些字节将存储在元素 b[off] 到 b[off+k-1] 中,而元素 b[off+k] 到 b[off+len-1] 不受影响。

在任何情况下,元素 b[0] 到 b[off] 和元素 b[off+len] 到 b[b.length-1] 都不受影响。

InputStream 类的 read(b, off, len) 方法只是重复调用方法 read()。如果第一个此类调用导致 IOException,则该异常会从对 read(b, off, len) 方法的调用中返回。如果对 read() 的任何后续调用导致 IOException,则捕获该异常并将其视为文件结尾;到目前为止读取的字节存储在 b 中,并返回发生异常之前读取的字节数。此方法的默认实现会阻塞,直到读取了请求的输入数据量 len、检测到文件结尾或抛出异常。鼓励子类提供此方法的更有效实现。

参数:

b - the buffer into which the data is read.

off - the start offset in array b at which the data is written.

len - the maximum number of bytes to read.

返回:

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

请仔细阅读最后一行。 -1 是一个特殊的返回值,表示没有数据被读取,因为InputStream 中没有可用的额外输入。

【讨论】:

  • 您好 Omajid,感谢您的回复。我还在学习编码,会处理 close(); .但我的问题是:文件的内容长度为 33996 字节。第一次读取,只读取 24356 个字节。所以我已经指示我的下一次读取从 24356 读取到 33996 ,即 9640 字节。但它没有给出 9640 输出,而是给出 -1
  • 某处存在错误。您在 InputStream 中读取的套接字没有为您提供数据。这可能是 Java 平台中的一个错误,但更有可能是网络的另一端正在中止连接。什么是发送这些数据?你能验证它正在发送所有数据吗?
猜你喜欢
  • 2012-01-01
  • 1970-01-01
  • 2016-09-15
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多