Android 读写文件 FileNotFoundException 异常

在尝试按照《Android开发艺术探索》中的示例使用Serializable接口时,遇到FileNotFoundException。问题并非权限问题,而是文件系统的读写限制。解决方案包括使用FileOutputStream带File对象的构造方法,并结合Android的getFilesDir()或getExternalFilesDir(null)方法获取可写路径。在模拟器中,可能因缺少外置存储导致错误,此时可能需要新建模拟器。
摘要由CSDN通过智能技术生成

转载请注明出处:http://blog.csdn.net/u013258802/article/details/53010157


照着《Android开发艺术探索》一书敲Serializable接口的例子时,遇到 FileNotFoundException 


例子很简单就是往文件里存个User对象:

User user = new User(0, "liang", true);
ObjectOutputStream out = null;
try {
  
    out = new ObjectOutputStream(new FileOutputStream("test.txt"));
    out.writeObject(user);
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}

这段抛出异常:FileNotFoundException: Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)

看起来感觉像缺少权限,不过跟权限没什么关系。


搜到一篇文章:

http://stackoverflow.com/questions/15711098/trying-to-create-a-file-in-android-open-failed-erofs-read-only-file-system


高赞回答说:

Please use the version of the FileOutputStream</

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值