繁体   English   中英

-1 在读取文件 java 时检测到

[英]-1 detected while reading file java

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,因此无法创建正确的文件。

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

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

public int read(byte[] b, int off, int len) 抛出 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没有可用的额外输入。

暂无
暂无

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

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