Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中写入/读取结构/类数据_Java_Android_File Io - Fatal编程技术网

Java 在android中写入/读取结构/类数据

Java 在android中写入/读取结构/类数据,java,android,file-io,Java,Android,File Io,基本上我想这样做 class myclass { int a1; float b1; char c1; //This is a single character } List<myclass> obs; 但是在Android操作系统中如何做到这一点呢?如果你想将你的类转储到文件中,让该类实现Serializable,java(或Android上的Dalvik VM)会在幕后为你做到这一点 class MyClass implements Serial

基本上我想这样做

class myclass
{
    int a1;
    float b1;
    char c1;     //This is a single character
}
List<myclass> obs;

但是在Android操作系统中如何做到这一点呢?

如果你想将你的类转储到文件中,让该类实现Serializable,java(或Android上的Dalvik VM)会在幕后为你做到这一点

class MyClass implements Serializable {

    private static final long serialVersionUID = 1L;

    int a1;
    float b1;
    char c1;     //This is a single character
}


private File file;
private MyClass myClass;

private void writeIt() throws IOException {

    ObjectOutputStream stream = null;
    try {
        stream = new ObjectOutputStream(new FileOutputStream(file));
        stream.writeObject(myClass);
    } 
    finally {
        if(stream != null) {
            stream.close();
        }
    }
}

private MyClass readIt() throws IOException, ClassNotFoundException {

    ObjectInputStream stream = null;
    try {
        stream = new ObjectInputStream(new FileInputStream(file));
        return (MyClass) stream.readObject();
    } 
    finally {
        if(stream != null) {
            stream.close();
        }
    }
}
class MyClass implements Serializable {

    private static final long serialVersionUID = 1L;

    int a1;
    float b1;
    char c1;     //This is a single character
}


private File file;
private MyClass myClass;

private void writeIt() throws IOException {

    ObjectOutputStream stream = null;
    try {
        stream = new ObjectOutputStream(new FileOutputStream(file));
        stream.writeObject(myClass);
    } 
    finally {
        if(stream != null) {
            stream.close();
        }
    }
}

private MyClass readIt() throws IOException, ClassNotFoundException {

    ObjectInputStream stream = null;
    try {
        stream = new ObjectInputStream(new FileInputStream(file));
        return (MyClass) stream.readObject();
    } 
    finally {
        if(stream != null) {
            stream.close();
        }
    }
}