简体   繁体   English

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

[英]-1 detected while reading file java

I am getting ambiguous outputs while reading from a server program in 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();

It is giving the following output for some files :它为某些文件提供以下输出:

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

The bytesRead in loop are -1 thereby it is not able to create the correct file.循环中的 bytesRead 为 -1,因此无法创建正确的文件。

Whenever you use a method, you should read its documentation to see what values it can return.每当你使用一个方法时,你应该阅读它的文档,看看它可以返回什么值。

Take a look at the description of InputStream.read(byte[], int, int) :看一下InputStream.read(byte[], int, int) 的描述

public int read(byte[] b, int off, int len) throws IOException public int read(byte[] b, int off, int len) 抛出 IOException

Reads up to len bytes of data from the input stream into an array of bytes.从输入流中读取最多 len 个字节的数据到一个字节数组中。 An attempt is made to read as many as len bytes, but a smaller number may be read.尝试读取多达 len 个字节,但可能会读取较小的数字。 The number of bytes actually read is returned as an integer.实际读取的字节数作为整数返回。

This method blocks until input data is available, end of file is detected, or an exception is thrown.此方法会阻塞,直到输入数据可用、检测到文件结尾或抛出异常。

If len is zero, then no bytes are read and 0 is returned;如果 len 为零,则不读取字节并返回 0; otherwise, there is an attempt to read at least one byte.否则,将尝试读取至少一个字节。 If no byte is available because the stream is at end of file, the value -1 is returned;如果由于流位于文件末尾而没有可用字节,则返回值 -1; otherwise, at least one byte is read and stored into b.否则,至少读取一个字节并存入 b。

The first byte read is stored into element b[off], the next one into b[off+1], and so on.读取的第一个字节存储到元素 b[off] 中,下一个存储到 b[off+1] 中,依此类推。 The number of bytes read is, at most, equal to len.读取的字节数最多等于 len。 Let k be the number of bytes actually read;令 k 为实际读取的字节数; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.这些字节将存储在元素 b[off] 到 b[off+k-1] 中,元素 b[off+k] 到 b[off+len-1] 不受影响。

In every case, elements b[0] through b[off] and elements b[off+len] through b[b.length-1] are unaffected.在每种情况下,元素 b[0] 到 b[off] 和元素 b[off+len] 到 b[b.length-1] 不受影响。

The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. InputStream 类的 read(b, off, len) 方法只是重复调用 read() 方法。 If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method.如果第一次此类调用导致 IOException,则该异常将从对 read(b, off, len) 方法的调用返回。 If any subsequent call to read() results in a IOException, the exception is caught and treated as if it were end of file;如果对 read() 的任何后续调用导致 IOException,则会捕获该异常并将其视为文件结尾; the bytes read up to that point are stored into b and the number of bytes read before the exception occurred is returned.到该点读取的字节存储到 b 中,并返回发生异常之前读取的字节数。 The default implementation of this method blocks until the requested amount of input data len has been read, end of file is detected, or an exception is thrown.此方法的默认实现会阻塞,直到读取了请求的输入数据量 len、检测到文件结尾或抛出异常。 Subclasses are encouraged to provide a more efficient implementation of this method.鼓励子类提供此方法的更有效实现。

Parameters:参数:

 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.

Returns:返回:

 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.

Read that last line very carefully.仔细阅读最后一行。 -1 is a special return value that means no data was read because there is no additional input available in the InputStream . -1 是一个特殊的返回值,这意味着没有读取数据,因为InputStream没有可用的额外输入。

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

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