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();
}
}
}