Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
-读取java文件时检测到1_Java_File_Byte - Fatal编程技术网

-读取java文件时检测到1

-读取java文件时检测到1,java,file,byte,Java,File,Byte,在java中读取服务器程序时,我得到了不明确的输出: byte [] mybytearray = new byte [content_length]; InputStream inputStream = clientSocket0.getInputStream(); //Getting input from peer0 FileOutputStream fileOutputStream = new FileOutputStream(fileDownloaded); //Sending t

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();
byte[]mybytearray=新字节[内容长度];
InputStream InputStream=clientSocket0.getInputStream()//获取输入
来自peer0
FileOutputStream FileOutputStream=新的FileOutputStream(fileDownloaded);
//将输出发送到本地目录
BufferedOutputStream BufferedOutputStream=新建
BufferedOutputStream(fileOutputStream);
ByteRead=inputStream.read(mybytearray,0,内容长度);
System.out.println(“首次读取:+bytesRead”);
当前=字节读取;
if(字节读取!=内容长度){
当前=字节读取;
做{
系统输出打印项次(当前+“-当前”);
System.out.println(“读取它:+(mybytearray.length current));
拜读=
读取(mybytearray,当前,(mybytearray.length当前));
System.out.println(“***”+字节读取);
如果(字节读==-1){
//当前=内容长度;
打破
}
其他的
电流+=字节读取;
}while(当前<内容\长度);
}
bufferedOutputStream.write(mybytearray,0,当前);
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
nfromserver0.close();
它为某些文件提供以下输出:

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


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

无论何时使用方法,都应阅读其文档,以查看它可以返回哪些值

请看以下内容的描述:

公共整数读取(字节[]b, int off, 内特兰) 抛出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

中没有其他可用输入。请正确设置代码格式!如果您使用的是最新版本的Java,请考虑使用try with resources,而不是手动调用
InputStream.close()
。我仍在学习编码,将负责close()。但我的问题是:文件的内容长度是33996字节。首先读取,仅读取24356字节。因此,我已指示下一次读取从24356读取到33996,即9640字节。但它并没有给出9640输出,而是给出-1。在某个地方有一个bug。您从InputStream中读取的套接字未向您提供数据。这可能是Java平台中的一个bug,但更有可能是网络的另一端正在中止连接。什么在发送这些数据?你能确认它正在发送所有数据吗?