2009-10-19 58 views
0

MyWriter.java(MyReader.java之前执行此)当写入OutputStream写入字节(内部)时,为什么读取的InputStream返回int?

import java.io.*; 
import java.net.*; 
import java.util.* ; 

public class MyWriter { 

    public static void main(String[] args) throws Exception { 

     ServerSocket ss = new ServerSocket(1234) ; 
     System.out.println("Server started...\n\n") ; 
     Socket s = ss.accept() ; 

     OutputStream out = s.getOutputStream() ; 

     Scanner scan = new Scanner(System.in) ; 

     while(true){ 
      System.out.print("Enter integer (or something else to quit) : "); 

      try{ 
       int i = scan.nextInt() ; 
       out.write(i) ; 
      }catch(RuntimeException rte){ 
       System.out.println("\n\n") ; 
       rte.printStackTrace() ; 
       System.out.println("\n\n") ; 
       break ; 
      } 
     } 
    } 
} 

MyReader.java(仅执行时,当与MyWriter.java完成)

import java.io.*; 
import java.net.*; 
import java.util.* ; 

public class MyReader { 

    public static void main(String[] args) throws Exception { 

     Socket s = new Socket("localhost", 1234) ; 

     InputStream is = s.getInputStream() ; 

     int i ; 
     while((i = is.read()) != -1){ 
      System.out.println("character form : " + (char)i + ", int form : " + i); 
     } 
    } 
} 

问题:MyReader即使我在MyWriter中传递-1,也不会终止。

+0

是否MyWriter终止?它应该打印一个堆栈跟踪,并且该过程应该退出。如果没有,那么套接字仍然打开,阅读器将阻止等待更多输入。 – erickson 2009-10-19 19:52:02

+0

它只会终止一个非整数值(异常处理不当,以使代码指向确切的问题)。否则,它在while(true)循环中。考虑一下,我的输入是只有一个可能的输入-1的整数,MyReader被终止,但它不会终止。 – mogli 2009-10-19 19:57:41

回答

3

阅读返回一个int,以便它也可以表示流的结束,为-1。如果签名是

byte read() 

然后你永远不会知道什么时候流已经结束(除非它抛出一个异常,我想)。

我怀疑没有特别的理由让write采取int - 这对我来说似乎是一个有点奇怪的选择。 Javadoc明确指出,高24位被忽略。也许这只是历史事故。

(在.NET中,Stream.ReadByte返回int,但Stream.WriteByte需要byte,这更有意义IMO)。现在

,有了这样的背景,当你写 “-1” 您的信息流,你”实际上写入值为“255” - 这就是你读回的内容。 “-1”在二进制中表示为“全1”,所以其低8位是“11111111”,当被认为是无符号值时,它是255。

只有当您关闭“书写”应用程序中的输出流时,您的读者才会终止 - 因为read()将返回-1。

+0

那么为什么这里MyReader没有终止-1输入。 – mogli 2009-10-19 19:58:47

+0

我正在编辑来解释这一点。 – 2009-10-19 20:00:21

+0

thnx先生Skeet :) – mogli 2009-10-19 20:04:00

0

找出代码有问题的最好方法是调试它,除了查看java文档。

在这种情况下,读取方法不会返回-1,除非它到达流的末尾,否则方法返回的值在0到255之间。

这是该方法的Java文档:

/** 
* Reads the next byte of data from the input stream. The value byte is 
* returned as an <code>int</code> in the range <code>0</code> to 
* <code>255</code>. If no byte is available because the end of the stream 
* has been reached, the value <code>-1</code> is returned. This method 
* blocks until input data is available, the end of the stream is detected, 
* or an exception is thrown. 
* 
* <p> A subclass must provide an implementation of this method. 
* 
* @return  the next byte of data, or <code>-1</code> if the end of the 
*    stream is reached. 
* @exception IOException if an I/O error occurs. 
*/ 

我已经为了方便调试重写类:

进口的java.io.InputStream; import java.net.Socket;

公共类MyReader {

public static void main(String[] args) throws Exception { 
    Socket s = new Socket("localhost", 1234); 
    InputStream is = s.getInputStream(); 

    int i; 
    do { 
     i = is.read(); 
     System.out.println("character form : " + (char) i + ", int form : " + i); 
    } while (i != -1); 
} 

}

乐趣;)

相关问题