2010-04-15 105 views
1

我有一個程序,我需要通過將它轉換爲字符串來將Class對象存儲到內存中。是否有可能將字符串轉換回原始類,以便我可以使用該類變量?我正在用JAVA做這個。在Java中將字符串轉換爲字符串,反之亦然

例子:test.java

class hello{ 
public String h1; 
public String h2; 
} 
public class test { 

public static void main(String[] args) 
{ 
    hello h = new hello(); 

    h.h1 = "hello"; 
    h.h2 = "world"; 

    String s = h.toString(); 
    System.out.println("Print s : "+s); 

    // Now I need to convert String s into type hello so that 
      // I can do this: 
      // (hello)s.h1; 
      // (hello)s.h2; 
} 
} 

注:這是不是一個功課,這是一個個人項目,我會如果有人能幫助感激不盡!

謝謝! Ivar

+0

什麼試圖用h.toString()? – 2010-04-15 04:07:45

+0

您不是將對象「投射」爲String,而是調用返回對象的String描述(通常用於調試/日誌記錄)的方法(toString())。這不是一個可逆的操作 - 按Derek的建議看待序列化。 – 2010-04-15 04:10:07

+0

請澄清:「需要將...存儲到內存中」?你的意思是需要堅持它/將它保存在磁盤文件或數據庫的某個地方嗎? – 2010-04-15 05:31:18

回答

2

我認爲你想要做的是序列化。我很困惑你的評論:

// Now I need to convert String s into type hello so that 
      // I can do this: 
      // (hello)s.h1; 
      // (hello)s.h2; 

你不能只是將String對象轉換爲任意類類型。也許你可以詳細說明你想在這裏完成什麼。如果您希望能夠將某個類「保存」到某個文件中,然後將其作爲對象重新讀入,則需要序列化。像這樣:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 

class Hello implements Serializable { 
    public String h1; 
    public String h2; 

    @Override 
    public String toString() { 
    return "{ h1: " + h1 + ", h2: " + h2 + " }"; 
    } 

    public static void main(String[] args) throws Exception { 
    Hello h = new Hello(); 

    h.h1 = "hello"; 
    h.h2 = "world"; 

    ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream("hello.ser")); 
    outstream.writeObject(h); 

    System.out.println("1) h: " + h); 

    h = null; 

    System.out.println("2) h: " + h); 

    ObjectInputStream instream = new ObjectInputStream(new FileInputStream("hello.ser")); 
    h = (Hello) instream.readObject(); 

    System.out.println("3) h: " + h); 

    } 
} 

當您的字段比String更復雜時,它會變得更加複雜。實現Serializable只是一個「標記」接口,它說對象可以被序列化,它不需要任何方法來實現。只需使用ObjectOutputStream寫出簡單的類,然後使用ObjectInputStream讀回。

+0

這是完美的答案!非常感謝!! – 2010-04-16 09:29:25

1

我認爲你需要做的是看看序列化/反序列化。

+0

工作!謝謝!! – 2010-04-16 09:28:09

0

如果您製作了對象hello的副本,那麼您不必擔心變回原始對象。

Hello h = new Hello(); 

Hello copyH = h; 
+0

謝謝大家的回覆!真的幫了很多!我現在是序列化的好朋友:) - ivar – 2010-04-16 09:26:31

0

我有一個程序,我需要 店Class對象到內存中

創建一個對象,它已經位於內存中,如果你希望緩存它,你可能會後放入HashMap中以供以後訪問。

將其轉換爲字符串。

沒有從字符串轉換爲對象,反之亦然。

是否有可能將字符串轉換 放回原類,這樣我 可以使用類變量?

Java序列是二進制的。要將對象狀態轉換爲字符串,您有幾個選項。

  • 打印成員的String和使用的StringTokenizer分割字符串並分配(這可能需要對非字符串的數據類型轉換)

  • 使用類似libcsv

  • 使用XML作爲對象狀態的字符串表示。在Java中,你可以使用我更喜歡的JAXB

0

您可以使用JavaBeans機制並實施PropertyEditor。你仍然需要自己實現字符串到對象的解析,如果你的對象很複雜,你需要在PropertyEditor中有很多邏輯來解析整個字符串。但是,當您準備好簡單類型的編輯器時,可以在更多comlex編輯器中使用這些編輯器。

當然,如果你不絕對需要你的對象的字符串表示,你應該考慮的序列化等建議。

+0

謝謝大家的回覆!真的幫了很多! 我現在是序列化的好朋友:) - ivar – 2010-04-16 09:26:15

0

如果你真的需要做這樣的事情,你可以覆蓋類的toString()方法,並創建一個靜態方法(或創建一個轉換器類),從它返回一個Hello對象的toString()。事情是這樣的:

class Hello() { 
    public String h1; 
    public String h2; 

    public String toString() { 
     return "h1:" + h1 +", h2:" + h2; 
    } 

    public static Hello fromString(String input) { 
     Hello hello = new Hello(); 
     hello.h1 = // Some logic to extract h1 from input 
     hello.h2 = // Some logic to extract h2 from input 

     return hello; 
    } 
} 

這樣,你應該能夠:

Hello hello = new Hello(); 
String helloString = hello.toString(); 
Hello newHello = Hello.fromString(helloString); 

我建議你檢查序列化(如許多其他有)或發現什麼是你真正的問題,解決的最好辦法它。

+0

序列化幫助我更好,謝謝您的解決方案! - 伊娃 – 2010-04-16 09:27:45

相關問題