在Red5中存储和检索共享对象

时间:2011-09-27 16:16:52

标签: java red5 shared-objects

我想要一个“简单”的方法,这样我就可以在磁盘中存储共享对象,然后即使red5服务器重新启动,我也可以从磁盘中检索这些文件。

PS:我浪费了很多时间才找到解释程序的好文档,但我找不到。

1 个答案:

答案 0 :(得分: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();
    }
}}