1

我用来XStreamxml file.
然后我再次反序列化文件以使用对象。
我的问题是,关闭程序后,xml“文件”消失了。那么如何将这个 xml 文件保存到特定目录呢?我已经尝试过FileOutputStream,但它不起作用......我也用谷歌搜索,但没有找到适合我的解决方案......

方法savePerson

public void savePerson(String uNummer, Person person) {
    System.out.println("save person");
    try{            
        xml = xstream.toXML(person);

    }catch (Exception e){
        System.err.println("Error in XML Write: " + e.getMessage());
    }
}

和方法readPerson

public Person readPerson(String uNummer) {
    System.out.println("read person");
     Person person = new Person();
    try{
        person = (Person) xstream.fromXML(file_path + uNummer + ".xml");       
    }catch(Exception e){
        System.err.println("Error in XML Read: " + e.getMessage());
    }
    return person;
}

目录\\releasearea\ToolReleaseArea\PersistenceSave

编辑
正确代码:(由ppeterka 提供

public void savePerson(String uNummer, Person person) {
    System.out.println("save person XML");
    FileOutputStream fos = null;
    try{            
        xml = xstream.toXML(person);
        fos = new FileOutputStream(file_path + uNummer + ".xml");
        fos.write("<?xml version=\"1.0\"?>".getBytes("UTF-8"));
        byte[] bytes = xml.getBytes("UTF-8");
        fos.write(bytes);

    }catch (Exception e){
        System.err.println("Error in XML Write: " + e.getMessage());
    }
    finally{
        if(fos != null){
            try{
                fos.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
4

3 回答 3

5

你没有写文件,只是获取了序列化的内容……

FileOutputStream fos = null;
try {
    fos = new FileOutputStream("myfilename");
    fos.write("<?xml version=\"1.0\"?>".getBytes("UTF-8")); //write XML header, as XStream doesn't do that for us
    byte[] bytes = xml.getBytes("UTF-8");
    fos.write(bytes);

} catch(Exception e) {
    e.printStackTrace(); // this obviously needs to be refined.
} finally {
    if(fos!=null) {
        try{ 
            fos.close();
        } catch (IOException e) {
            e.printStackTrace(); // this obviously needs to be refined.
        }
    }
}

此外,您的阅读功能也有错误:xstream.fromXML(String)接受 a String,但它不会将其解释为文件名,而是将其解释为 XML 内容本身......您必须使用该fromXML(File)功能:

public Person readPerson(String uNummer) {
    System.out.println("read person");
    Person person = new Person(); //if there is an error during deserialization, this is going to be returned, is this what you want?
    try{
        File xmlFile = new File(file_path + uNummer + ".xml");
        person = (Person) xstream.fromXML(xmlFile);       
    }catch(Exception e){
        System.err.println("Error in XML Read: " + e.getMessage());
    }
    return person;
}
于 2012-10-25T07:48:42.970 回答
5

使用重载方法 toXML(Object o, Writer w) 直接序列化到文件。您使用的 toXML 方法不会保存到文件中。

xstream.toXML(person, new FileWriter(file));
于 2012-10-25T07:53:59.213 回答
0

我这样做效果很好:

//Your Stream things...

    String xml = xstream.toXML(type);

        System.out.println(xml);

        BufferedReader reader = new BufferedReader(new StringReader(xml));
        BufferedWriter writer = new BufferedWriter(new FileWriter("test.xml",
                true));

        while ((xml = reader.readLine()) != null) {

            writer.write(xml + System.getProperty("line.separator"));

        }

        writer.close()

;

输出是:

<type>
  <OBJECT__TYPE>sdfsdf</OBJECT__TYPE>
  <prop>
    <DESCRIPTION>hfh</DESCRIPTION>
    <PARENT>NULL</PARENT>
    <VIRTUAL>0</VIRTUAL>
    <VISIBLE>1</VISIBLE>
    <PICTURE>NULL</PICTURE>
    <HELP>345345</HELP>
    <MIN__NO>NULL</MIN__NO>
    <MAX__NO>1</MAX__NO>
    <NAME__FORMAT>NULL</NAME__FORMAT>
  </prop>
</type>
<type>
  <OBJECT__TYPE>test</OBJECT__TYPE>
  <prop>
    <DESCRIPTION>test</DESCRIPTION>
    <PARENT>NULL</PARENT>
    <VIRTUAL>0</VIRTUAL>
    <VISIBLE>0</VISIBLE>
    <PICTURE>NULL</PICTURE>
    <HELP>noe</HELP>
    <MIN__NO>NULL</MIN__NO>
    <MAX__NO>NULL</MAX__NO>
    <NAME__FORMAT>5475</NAME__FORMAT>
  </prop>
</type>
于 2014-01-23T13:19:18.087 回答