把string和int型类型转成byte[]类型通过流发送,在客户方如何解码? 放在一起就成String了,除非你记住第几个字节到第几个字节是String,第几个字节到第几个字节是int 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以通过ObjectInputStream试一下,把int 转成Integer,这样传送String和Integer对象,另一端用OjbectOutputStream来接收,一定可以。 那我怎么把int取出来啊!我知道第几个字节是int... 我要发送的服务方是用C写的,所以,它只能接收byte流... 给你一个读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;} 很简单:遵守如下规则: 发送端用java.io.DataOutputStream来写流,其中,方法writeBytes(String s)和方法writeInt(int v)可以直接把String和int写成byte。 接收端用java.io.DataInputStream来读流,其中,方法readLine和readInt可以直接读取byte流并转换为你的需要的类型。 对于String,你可以直接尝试writeUTF和readUTF如果你觉得readLine不方便。(因为这个方法被deprecated) 既然你知道第几个字节是int型。那就用把int转换成String 传递然后再在客户端接收String然后截取你确定是int型的String,转成int。 如果有两个整型需要发送...接收端如何进行解码?用readInt()后再用一次readInt()吗? 对呀。你客户端写写流是依照一定次序的。接受端再依照这个次序用DataInputStream读出来。非常管用。我刚实验过。另外,补充一下,如果你写字符串,用writeUTF和readUTF 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(); }} 我的发送方如下: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都没有取出来....怎么回事????????? 哦,我知道了...我写的时候用了writeBytes,读的时候用的readUTF,所以出现了上述问题...如果我用writeInt(),写到流里的是几个字节啊??我现在想要固定格式,例如:int a发送的时候用4个字节...int b发送的时候用2个字节...如何实现? 调用 writeChar( yourInt ),写两个字节。主要问题都已经解决。具体细节参看类的说明文档吧。Good Luck! Exception in thread "main" java.lang.NoSuchMethodError: main 三目运算符的问题,感觉有点问题。 JFrame 画线问题 java中数组问题 构造函数前面有public和啥也没有,有什么区别,感谢 getContentPane()这个方法有什么用啊?? 读文件读到回车时的奇怪问题 如何使用calendar类提出这个日期变量的一部分值? java中的输入小问题,急急急急 请问如何能让JButton里的字占满整个按钮的大小? 定时连接oracle查找记录,但是出错!请各位高手指点,我太菜了~~:) 高手救急:两个数组怎么相加?!
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;
}
遵守如下规则:
发送端用java.io.DataOutputStream来写流,其中,方法writeBytes(String s)和方法writeInt(int v)可以直接把String和int写成byte。
接收端用java.io.DataInputStream来读流,其中,方法readLine和readInt可以直接读取byte流并转换为你的需要的类型。
对于String,你可以直接尝试writeUTF和readUTF如果你觉得readLine不方便。(因为这个方法被deprecated)
然后截取你确定是int型的String,转成int。
用readInt()后再用一次readInt()吗?
你客户端写写流是依照一定次序的。
接受端再依照这个次序用DataInputStream读出来。非常管用。我刚实验过。
另外,补充一下,如果你写字符串,用writeUTF和readUTF
{
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();
}}
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都没有取出来....
怎么回事?????????
如果我用writeInt(),写到流里的是几个字节啊??
我现在想要固定格式,例如:int a发送的时候用4个字节...int b发送的时候用2个字节...如何实现?