2

我想以树状形式将实例的内容发送给某人。打印屏幕不行,因为这个类太复杂了。

4

2 回答 2

2

您是否需要将输出转回实例?在这种情况下,其他答案是正确的。

如果您只想手动检查实例的内容,理想情况下您的类将全部实现toString(),您可以将其重定向到文件。

如果你没有好的 toStrings,你可以使用下面的(例如在 Eclipse 的 Expressions 视图中)

org.apache.commons.io.FileUtils.writeStringToFile(
    new java.io.File("C:\\objectAsString.txt"),
    org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString(
    variableToInspect,
    org.apache.commons.lang3.builder.ToStringStyle.MULTI_LINE_STYLE))

如果您在类路径上有 commons-lang3 和 commons-io。

于 2012-05-17T08:15:55.050 回答
1

you could use Xml parsing or serialization as vidhya just said.

Xml more reflexive, it is easier to use or read in any type of program, you won't have versioning problems and many many other good features.

Serializing can be easier if you use this exact application on the other end. but many bugs can find their way into your application.

for xml parsing you can use java XML library or many third party libraries like Dom4j. Xml parsing is easy enough, You can add a toXml and fromXml to each one of your entities, and define how you would like that entity being parsed.

于 2012-05-17T04:12:05.990 回答