writeUTF(String s)vs writeObject(String s) [英] writeUTF(String s) vs writeObject(String s)

查看:171
本文介绍了writeUTF(String s)vs writeObject(String s)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我上大学的这个Java项目中,我遇到一种情况,当前正在使用

In this Java project I'm working on for university, I have a situation where I am currently sending strings through the network successfully using

streamOut = ObjectOutputStream
streamIn = ObjectInputStream

streamOut.writeUTF(msgs.peek());

其中msgs是一个链接的阻塞队列,通过

where msgs is a linked blocking queue, receiving it with

String in = streamIn.readUTF();

但是,我想使用ObjectInputStream和ObjectOutputStream。我已经在构造函数中初始化了它们,并在构造它后刷新了ObjectOutputStream,我读到某个地方必须这样做。

however, I would like to use an ObjectInputStream and an ObjectOutputStream. I have initialized them both in the constructor and I flush the ObjectOutputStream after constructing it, I read somewhere you have to do this.

我想同时发送Strings和另一个Object类型,通过网络将其称为gameWorld(此时不关心效率)。但是,当我这样做

I want to send both Strings and another Object type, call it gameWorld over the network (don't care about efficiency at this point).. however when I do

streamOut.writeObject("mad cuz i'm bad");

Object in = streamIn.readObject(); 
if(in instanceof String) String inS = (String) in;

当我发送字符串时,它没有捡到任何东西...我的朋友正在相同的项目,并且他只传递一种对象,该对象的子类之一本质上是字符串,并且他的版本工作正常,但是他在线程的运行循环的每次迭代中都创建了一个新流。

it doesn't pick anything up when I send strings over... my friend is working on the same project and he passes around only 1 type of object, one of the subclasses of this object is essentially a string and his version works fine, but he makes a new stream in every iteration of his thread's run loop.

我是否需要对流进行某些操作以接收除Object之外没有其他共同祖先的其他对象,我是否需要在运行循环的每次迭代中创建一个新的流,或者

Do I need to do something with the stream to receive different objects which don't have a common ancestor other than Object, do I need to make a new stream every iteration of the run loop or is there just something else completely that I'm missing and the information I've provided isn't sufficient to tell whats wrong?

推荐答案

将String作为原始数据或对象写入流之间存在显着差异。首先,将writeObject编写的String实例作为String写入流中。将来的writeObject()调用将对字符串的引用写入流中。

There is a significant difference between writing a String into the stream as primitive data or as an Object. A String instance written by writeObject is written into the stream as a String initially. Future writeObject() calls write references to the string into the stream.

例如

    ByteArrayOutputStream baos1=new ByteArrayOutputStream();
    oos1=new ObjectOutputStream(baos1);
    baos2=new ByteArrayOutputStream();
    ObjectOutputStream oos2=new ObjectOutputStream(baos2);
    String testString="First";
    oos1.writeObject(testString);
    oos2.writeUTF(testString);
    testString="Second";
    oos1.writeObject(testString);
    oos2.writeUTF(testString);
    testString="Third";
    oos1.writeObject(testString);
    oos2.writeUTF(testString);
    oos1.flush();
    oos2.flush();
    byte[] byteArray1=baos1.toByteArray();
    byte[] byteArray2=baos2.toByteArray();

转储最后两个数组,您将得到如下结果:

writeObject ,即byteArray1

二进制:-84 -19 0 5 116 0 5 70 105 114 115 116 116 0 6 83 101 99 111 110 100 116 0 5 84 104 105 114 100

ASCII:-T- t 优先 t S econd t Third

Dump last two arrays you will get result like:
writeObject i.e. byteArray1
Binary: -84 -19 0 5 116 0 5 70 105 114 115 116 116 0 6 83 101 99 111 110 100 116 0 5 84 104 105 114 100
ASCII: -T - t F i r s t t S e c o n d t T h i r d

writeUTF ,即byteArray2

二进制:-84 -19 0 5 119 22 0 5 70 105 114 115 116 0 6 83101 99 111 110 100 0 5 84 104 105 114 100

ASCII:-T- w 优先级第三级

writeUTF i.e. byteArray2
Binary: -84 -19 0 5 119 22 0 5 70 105 114 115 116 0 6 83 101 99 111 110 100 0 5 84 104 105 114 100
ASCII: -T - w F i r s t S e c o n d T h i r d

结论:如果使用writeObject,则要传输额外的数据(此处为 t ),而如果使用writeUTF,则仅要传输字符串数据。

Conclusion: In case of writeObject an extra data (here t)to be streamed whereas in case of writeUTF only the string data to be streamed.

更多信息: http://docs.oracle.com/javas e / 7 / docs / api / java / io / ObjectOutputStream.html#writeUTF(java.lang.String)

这篇关于writeUTF(String s)vs writeObject(String s)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆