DataInputStream in = new DataInputStream(new BufferedInputStream(you.getInputStream()));

DataInputStream in = new DataInputStream((you.getInputStream()));
有什么区别?为什么当用第一个的时候,in.readUTF()读不到数据,但用第二个的时候,in.readUTF()则可以读到数据?
 注: out和in对应,使用的方式是:
服务器
String str = "what the hell";
out.writeUTF(str);
客户端
String str = in.readUTF();

解决方案 »

  1.   

    DataInputStream in = new DataInputStream(new BufferedInputStream(you.getInputStream())); 相当于使用数据输入流的时候外面再加了一个处理流BufferedInputStream第一个in.readUTF()读不到数据可能出现的情况就是BufferedInputStream的缓冲大于实际的数据长度...你没具体程序,我只能说一下我自己的理解.第二个in.readUTF可以读到数据是因为它是以字节方式读取的,只要你存在字节,就能读到东西.
      

  2.   

    out.writeUTF(str); 
    out.flush();加上这个flush看看效果
      

  3.   

    DataInputStream in = new DataInputStream(new BufferedInputStream(you.getInputStream())); 使用这个的时候,我没有定义缓冲区,难道每次还要根据字符串长度来定义缓冲区大小么?能不能给我一个用以上方式定义in的一个例子?
      

  4.   

       谢谢java2000_net,不过我试了一下,还是没用。效果是一样的:就好像没有out.writeUTF一样,那边一直在等待的输入的感觉,CPU利用率也不高。
      

  5.   

    第一个带缓冲的,要flush or close