2008-12-02 105 views

回答

0

是否有任何特別的原因使用XStream?如果你正在做的是試圖序列化一個或兩個字符串,這將是非常容易做的,如JDOM

即,類似於: Document doc = new Document();

Element rootEl = new Element("root"); 
rootEl.setText("my string"); 
doc.appendChild(rootEl); 
XMLOutputter outputter = new XMLOutputter(); 
outputter.output(doc); 

上面的一些細節可能是錯誤的,但那就是基本流程。也許你應該問一個更具體的問題,以便我們能夠確切地理解你所面臨的問題是什麼?

0

http://www.xml.com/pub/a/2004/08/18/xstream.html

import com.thoughtworks.xstream.XStream; 

class Date { 
    int year; 
    int month; 
    int day; 
} 

public class Serialize { 
    public static void main(String[] args) { 

     XStream xstream = new XStream(); 

     Date date = new Date(); 
     date.year = 2004; 
     date.month = 8; 
     date.day = 15; 

     xstream.alias("date", Date.class); 

     String decl = "\n"; 

     String xml = xstream.toXML(date); 

     System.out.print(decl + xml); 
    } 
} 

public class Deserialize { 

    public static void main(String[] args) { 

     XStream xstream = new XStream(); 

     Date date = new Date(); 

     xstream.alias("date", Date.class); 

     String xml = xstream.toXML(date); 

     System.out.print(xml); 

     Date newdate = (Date)xstream.fromXML(xml); 
     newdate.month = 12; 
     newdate.day = 2; 

     String newxml = xstream.toXML(newdate); 

     System.out.print("\n\n" + newxml); 
    } 
} 

然後,您可以採取的XML字符串,並將其寫入文件。

4

如果您可以將它序列化爲一個txt文件,只需打開一個ObjectOutputStream並讓它使用String自己的序列化功能。

String str = "serialize me"; 
    String file = "file.txt"; 
    try{ 
     ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); 
     out.writeObject(str); 
     out.close(); 

     ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); 
     String newString = (String) in.readObject(); 
     assert str.equals(newString); 
     System.out.println("Strings are equal"); 
    }catch(IOException ex){ 
     ex.printStackTrace(); 
    }catch(ClassNotFoundException ex){ 
     ex.printStackTrace(); 
    } 

你也可以只打開一個PrintStream和虹吸出來這種方式,然後使用一個BufferedReader和readline()。如果你真的想變得有趣(因爲這是一個HW分配),你可以使用for循環並單獨打印每個字符。使用XML比你需要序列化一個字符串更復雜,而使用外部庫只是矯枉過正。

5

XStream具有讀取和寫入文件的功能,請參閱簡單示例(Writer.java和Reader.java)in this article

+2

+1,我使用:xstream.toXML(myThing,新的FileOutputStream中(新文件( 「將myfile.xml」))); – teejay 2013-05-07 18:51:02

1

如果你需要創建一個包含XML表示對象的內容的文本文件(並使其雙向),只需使用JSON-lib目錄下:

class MyBean{ 
    private String name = "json"; 
    private int pojoId = 1; 
    private char[] options = new char[]{'a','f'}; 
    private String func1 = "function(i){ return this.options[i]; }"; 
    private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];"); 

    // getters & setters 
    ... 
} 

JSONObject jsonObject = JSONObject.fromObject(new MyBean()); 
String xmlText = XMLSerializer.write(jsonObject); 

從那裏只寫字符串到文件。比所有這些XML API簡單得多。但是,現在,如果您需要符合DTD或XSD,那麼這是一種不好的方式,因爲它的格式更自由,並且只符合對象佈局。

http://json-lib.sourceforge.net/usage.html

Piko的

3

如果你開始爪哇,然後需要一些時間通過Apache Commons項目的樣子。 Java有很多基本的擴展,你會多次使用。

我假設你只是想保留一個字符串,以便以後可以讀回來 - 在這種情況下,它不一定需要是XML。

要將字符串寫入文件,請參閱org.apache.commons.io。文件實用程序:

FileUtils.writeStringToFile(File file,String data)

讀回:

FileUtils.readFileToString(File file)

參考文獻:

http://commons.apache.org/

http://commons.apache.org/io

http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html

請確保你也看看commons-lang有很多很好的基本東西。

0

嘗試這樣的事:

FileOutputStream fos = null; 
    try { 
     new File(FILE_LOCATION_DIRECTORY).mkdirs(); 
     File fileLocation = new File(FILE_LOCATION_DIRECTORY + "/" + fileName); 
     fos = new FileOutputStream(fileLocation); 
     stream.toXML(userAlertSubscription, fos);     
    } catch (IOException e) { 
     Log.error(this, "Error %s in file %s", e.getMessage(), fileName); 
    } finally { 
     IOUtils.closeQuietly(fos); 
    }