使用 FileInputStream(适用于 Java)读取 XML 文件?
对于我的项目,我必须使用 Java 和 XStream 序列化和反序列化随机树。 我的老师制作了 Tree/RandomTree 算法,所以我不必担心这一点。 我不知道该怎么做是这样的:我正在使用 FileInputStream 读取/写入我序列化和反序列化的 xml 文件,但是当我反序列化时,我不知道用于读取该文件的方法。 读取文件后,我应该能够将其从 XML 转换,然后将其作为字符串打印出来。 这是我到目前为止所拥有的。 (我正确导入了所有内容,只是没有将其添加到我的代码段中)。
FileInputStream fin;
try
{
// Open an input stream
fin = new FileInputStream ("/Users/Pat/programs/randomtree.xml");
//I don't know what to put below this, to read FileInpuStream object fin
String dexml = (String)xstream.fromXML(fin);
System.out.println(dexml);
// Close our input stream
fin.close();
System.out.println(dexml);
// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
编辑:我想通了; 我认为我不必将其打印为字符串,我只需要制作一个基准测试框架来对其进行计时等,但再次感谢!
For my project I have to serialize and deserialize a random tree using Java and XStream. My teacher made the Tree/RandomTree algorithms, so I don't have to worry about that. What I don't know how to do is this: I am using FileInputStream to read/write the xml file that I serialized and deserialized, but when I deserialize, I do not know the method used to read the file. After I read the file I should be able to convert it from XML and then print it out as a string. Here's what I have so far. (I imported everything correctly, just didn't add it to my code segment).
FileInputStream fin;
try
{
// Open an input stream
fin = new FileInputStream ("/Users/Pat/programs/randomtree.xml");
//I don't know what to put below this, to read FileInpuStream object fin
String dexml = (String)xstream.fromXML(fin);
System.out.println(dexml);
// Close our input stream
fin.close();
System.out.println(dexml);
// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
Edit: I figured it out; I don't think I have to print it as a string, I just needed to make a benchmarking framework to time it and such, but thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xstream.fromXML()
方法将为您从输入流中读取数据。 我认为问题在于,您将 xstream.fromXML(fin) 的返回值转换为字符串,而实际上它应该转换为您最初序列化的对象类型(RandomTree 我认为)。 所以代码看起来像这样:
编辑:在注释中澄清之后,作者的目标是首先读入一个字符串,以便可以在反序列化之前打印 XML 内容。 考虑到这一目标,我建议查看 此线程
The
xstream.fromXML()
method will do the reading from the input stream for you. I think the problem is that you are casting the return value fromxstream.fromXML(fin)
into a String when it should be cast to the type of object you originally serialized (RandomTree
I assume). So the code would look like this:EDIT: after clarification in comments, the author's goal is to first read into a String so the XML contents can be printed before deserialization. With that goal in mind, I recommend taking a look at the IOUtils library mentioned in this thread
据我了解 http://x-stream.github.io/tutorial.html (我以前从未使用过 XStream),您需要首先定义您的类型。 转换为字符串肯定是错误的,您可能需要自定义类型(取决于随机 XML 中的内容),然后您需要将 XML 标签映射到您的成员:
例如
,这意味着它将 XML 中的“person”标签映射到您的 成员。人物类。
要反序列化,您可以执行以下操作:
此外,您将关闭流两次,并且您可能希望在finally块中执行此操作:)
编辑:阅读上面的评论...
您的任务涉及两个步骤:
为了序列化您的对象,您必须首先从输入文件中反序列化它。
要将对象输出为字符串,只需执行以下操作
From what I understand from http://x-stream.github.io/tutorial.html (I've never worked with XStream before), you need to define your types first. Casting to String is definitely wrong, you probably want a customized type (depending on what's inside your random XML), then you need to map the XML tags to your members:
e.g.
meaning that it maps the "person" tag inside your XML to your Person class.
To derserialize, you can do:
Also, you are closing your stream twice, and you probably want to do it in a finally block :)
edit: Having read your comment above...
Your task involves two steps:
In order to serialize your object, you must deserialize it first from your input file.
To output your Object as String, simply do