使用XStream保存XML文件

Mic*_*idt 1 java xstream

XStream用来写一个对象xml 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());
    }
}
Run Code Online (Sandbox Code Playgroud)

而方法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;
}
Run Code Online (Sandbox Code Playgroud)

目录:\\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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ppe*_*rka 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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您的阅读功能也有错误: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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这只是一种数据类型转换.在写入文件之前,必须将String转换为字节.明确指定字符编码是明智的,因为默认编码可能因系统而异. (2认同)

sha*_*zin 5

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

xstream.toXML(person, new FileWriter(file));
Run Code Online (Sandbox Code Playgroud)