2011-09-27 48 views
1

我想要一個「簡單」方法,這樣我就可以將共享對象存儲在磁盤中,然後我可以從磁盤中檢索這些,即使red5服務器重新啓動。 PS:我浪費了好幾個小時才找到解釋程序的好文檔,但是我找不到任何文檔。在Red5中存儲和檢索共享對象

回答

0

只需注意在你的對象的每個字段應該是序列化的,那麼你可以參考此代碼示例:

import java.io.Serializable; 

@SuppressWarnings("serial") 
public class Person implements Serializable{ 
private String name; 
private int age; 
public Person(){ 

} 
public Person(String str, int n){ 
    System.out.println("Inside Person's Constructor"); 
    name = str; 
    age = n; 
} 
String getName(){ 
    return name; 
} 
int getAge(){ 
    return age; 
}} 

**

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


public class SerializeToFlatFile { 
public static void main(String[] args) { 
    SerializeToFlatFile ser = new SerializeToFlatFile(); 
    ser.savePerson(); 
    ser.restorePerson();   
} 

public void savePerson(){ 
    Person myPerson = new Person("Jay",24); 
    try { 
     FileOutputStream fos = new FileOutputStream("E:\\workspace\\2010_03\\src\\myPerson.txt"); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     System.out.println("Person--Jay,24---Written"); 
     System.out.println("Name is: "+myPerson.getName()); 
     System.out.println("Age is: "+myPerson.getAge()); 

     oos.writeObject(myPerson); 
     oos.flush(); 
     oos.close(); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
} 

public void restorePerson() { 
    try { 
     FileInputStream fis = new FileInputStream("E:\\workspace\\2010_03\\src\\myPerson.txt"); 
     ObjectInputStream ois = new ObjectInputStream(fis); 

     Person myPerson = (Person)ois.readObject(); 
     System.out.println("\n--------------------\n"); 
     System.out.println("Person--Jay,24---Restored"); 
     System.out.println("Name is: "+myPerson.getName()); 
     System.out.println("Age is: "+myPerson.getAge()); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
}} 
+0

感謝您的評論,但我說的Red5服務器。 –

+0

非常抱歉,我以爲你在談論Redhat。 ( - - |)不熟悉Red5,希望你得到一個很好的答案。 – Aloong