开发者

Storing and retrieving Shared objects in Red5

I want a "simple" me开发者_如何学运维thod so i can store shared objects in the disk, and then i can retrieve these so's from the disk even the red5 server restarts.

PS: i wasted many hours on finding good doc that explains the procedure but i found none.


Just notice that every field in your object should be serializable, then you can refer to this code sample:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜