基本上我想这样做,
class myclass
{
int a1;
float b1;
char c1; //This is a single character
}
List<myclass> obs;
现在在运行时这个obs变量因为它是一个列表将包含myclass实例的数组
其大小我们得到obs.size();
那么,如何将这些数据写入文件,将“data1.bin”称为二进制,使用OutputStream或类似的东西。但这要在Android OS中完成。
我在c ++中做了类似的事情,比如
class myclass
{
int a1;
float b1;
char c1;
}
myclass student1;
ofstream output_file("students.data", ios::binary);
output_file.write((char*)&student1, sizeof(student1));
output_file.close()
但是如何在Android OS中执行此操作?
答案 0 :(得分:0)
如果你想把你的类转储到文件,让类实现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();
}
}
}