我试过这个:
public class People implements Serializable {
String name;
public People(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void saveAsObject(People p) throws FileNotFoundException, IOException
{
try
{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("test","test.dat") ));
os.writeObject(p);
os.close();
System.out.println("Sucess");
}
catch(IOException e)
{
System.out.println(e);
}
}
}
我手动创建了该文件夹。它抛出这个错误:“java.io.FileNotFoundException:/test/test.txt:打开失败:ENOENT”(没有这样的文件或目录)
当我尝试
new FileOutputStream("text.dat")
它抛出 java.io.FileNotFoundException: EROFS (只读文件系统)
这是我的主要课程:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
People p = new People("Ray");
try {
p.saveAsObject(p);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}