我试过这个:
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: open failed: 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();
}
}发布于 2013-02-25 22:39:10
此new File("test","test.dat")不是在Android上获取文件路径的有效方式。
试试这个:
new File(Environment.getExternalStorageDirectory(), "/data.dat");发布于 2013-02-25 23:59:28
你的应用默认的当前工作目录是"/",你没有写的权限。
https://stackoverflow.com/questions/15069368
复制相似问题