如何将变量的整个内容发送/导出到文本文件/ xml文件/剪贴板? [英] How can I send/export the whole content of a variable to a text file/xml file/clipboard?

查看:172
本文介绍了如何将变量的整个内容发送/导出到文本文件/ xml文件/剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to send the content of an instance, in tree-form preferrably, to somebody. A print screen will not do because the class is too complex.

推荐答案

您需要将输出恢复为实例吗?在这种情况下,其他的答案就是现场。

Do you need to turn the output back into an instance? In that case, the other answers are spot on.

如果您只想手动检查实例的内容,最好您的类将实现 toString()您可以将其重定向到文件。

If you just want to manually inspect the contents of the instance, ideally your classes would all implement toString() and you could redirect this to a file.

如果你没有很好的toString,你可以使用以下内容(例如在Eclipse的表达式视图中)

If you haven't got good toStrings, you can use the following (e.g. in Eclipse's Expressions view)

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。

if you have commons-lang3 and commons-io on the classpath.

这篇关于如何将变量的整个内容发送/导出到文本文件/ xml文件/剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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