English 中文(简体)
2. 输出物体
原标题:Serialize object with outputstream

附录一有<条码>OutputStream(,而不是>>。 是否可能使用书写方法发送一个序列化物体? 感谢!

最佳回答

这里是你对物体的序列化:

new ObjectOutputStream(outputStream).writeObject(obj);

如果您想要控制<代码>逐[ ]输出:

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(buffer);

oos.writeObject(obj);

oos.close();

byte[] rawData = buffer.toByteArray();
问题回答

You could use ObjectOutputStream to capture the objects data in a byte Array and send this to the OutputStream.

String s = "test";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( s );
byte[] byteArray = baos.toByteArray();
for ( byte b : byteArray ) {
    System.out.print( (char) b );
}

另一种非通用选择是将物体按方位代表顺序排列。 CSV

This is trivial: you can simply wrap your original OutputStream in a new ObjectOutputStream, and then use the specialized methods of ObjectOutputStream:

OutputStream myOriginalOutputStream = ...;
ObjectOutputStream oos = new ObjectOutputStream(myOriginalOutputStream);
oos.writeObject(new MyObject());
oos.flush();
oos.close();

Internally, ObjectOutputStream will call the underlying OutputStream s write() method.

你们必须使用。 目标OutputStream 类别及其<代码>* 编码* 物体的方法。 事实上,ObjectOutputStreamjava.io.OutputStream的子类别。 (这是面向次流的抽象超级类别。) 参看的文章。 Java Serialization AP.

EDIT: You can use XMLEncoder

(from the Doc : The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签