放在一起就成String了,除非你记住第几个字节到第几个字节是String,第几个字节到第几个字节是int

解决方案 »

  1.   

    可以通过ObjectInputStream试一下,把int 转成Integer,这样传送String和Integer对象,另一端用OjbectOutputStream来接收,一定可以。
      

  2.   

    那我怎么把int取出来啊!我知道第几个字节是int...
      

  3.   

    我要发送的服务方是用C写的,所以,它只能接收byte流...
      

  4.   

    给你一个读int的例子,以下假定传来的int按网络字节顺序(高位在前) 
     
    public  int rdIntV(byte [] b, int off)
    {
         int i = (((b[off] & 0Xff) << 24) |
                  ((b[off+1] & 0Xff) << 16) |
                    ((b[off+2] & 0Xff) << 8) |
                    (b[off+3] & 0Xff));
          return i;
    }
      

  5.   

    很简单:
    遵守如下规则:
    发送端用java.io.DataOutputStream来写流,其中,方法writeBytes(String s)和方法writeInt(int v)可以直接把String和int写成byte。
    接收端用java.io.DataInputStream来读流,其中,方法readLine和readInt可以直接读取byte流并转换为你的需要的类型。

    对于String,你可以直接尝试writeUTF和readUTF如果你觉得readLine不方便。(因为这个方法被deprecated)
      

  6.   

    既然你知道第几个字节是int型。那就用把int转换成String 传递然后再在客户端接收String
    然后截取你确定是int型的String,转成int。
      

  7.   

    如果有两个整型需要发送...接收端如何进行解码?
    用readInt()后再用一次readInt()吗?
      

  8.   

    对呀。
    你客户端写写流是依照一定次序的。
    接受端再依照这个次序用DataInputStream读出来。非常管用。我刚实验过。
    另外,补充一下,如果你写字符串,用writeUTF和readUTF
      

  9.   

    public class TestData
    {
        java.io.FileInputStream fs;
        java.io.FileOutputStream fo;
        public TestData()
        {
        }
        public void write()
        {
            try
            {
                fo = new java.io.FileOutputStream("k.txt");
                String s="hello!";
                java.io.DataOutputStream dos = new java.io.DataOutputStream(fo);
                dos.writeByte(1);
                dos.writeUTF(s);
                dos.writeUTF("KK");
                dos.writeByte(2);
                dos.close();
            }catch(Exception ex)
            {
                ex.printStackTrace();
           }    }
        public void read()
        {
            try
            {
                fs = new java.io.FileInputStream("k.txt");
                java.io.DataInputStream dos = new java.io.DataInputStream(fs);
                System.out.println(dos.readByte());
                System.out.println(dos.readUTF());
                System.out.println(dos.readUTF());
                int v2 = dos.readByte();
                System.out.println(v2);
                dos.close();
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        public static void main(String[] args)
        {
            TestData t = new TestData();
            t.write();
            t.read();
        }}
      

  10.   

    我的发送方如下:
    DataOutputStream dos=null;
    ...
    String hello="abcdefg";
    int a=2;
    int b=3;
    dos.writeBytes(hello);
    dos.writeInt(a);
    dos.writeInt(b);我的接收方如下:DataInputStream dis=null;
    ...
    String str=dis.readUTF();
    int result=dis.readInt();
    int re=dis.readInt();System.out.println("String = "+str);
    System.out.println("a = "+result);
    System.out.println("b = "+re);结果String,a,b都没有取出来....
    怎么回事?????????
      

  11.   

    哦,我知道了...我写的时候用了writeBytes,读的时候用的readUTF,所以出现了上述问题...
    如果我用writeInt(),写到流里的是几个字节啊??
    我现在想要固定格式,例如:int a发送的时候用4个字节...int b发送的时候用2个字节...如何实现?
      

  12.   

    调用 writeChar( yourInt ),写两个字节。主要问题都已经解决。具体细节参看类的说明文档吧。Good Luck!