gpt4 book ai didi

java - 打开一个存在的文件时出现 FileNotFoundException?

转载 作者:行者123 更新时间:2023-11-30 09:21:32 25 4
gpt4 key购买 nike

我创建了一个文件:

FileOutputStream fos = app.openFileOutput("FileOne.txt", Context.MODE_APPEND);

稍后在同一个程序中,我尝试使用以下方法打开此文件:

PrintWriter writer = new PrintWriter(context.getFileStreamPath("FileOne.txt"));

但是这样做会在 LogCat 中给出这个异常:

06-05 09:49:32.230: D/error155 java.lang.RuntimeException: File not found(7707): [ 06-05 09:49:32.240  7707: 7736 D/ java.io.FileNotFoundException: /FileOne.txt: open failed: EROFS (Read-only file)

我检查了内部存储,文件确实存在。如何正确打开文件?

最佳答案

File file = new File(你的路径);

//首先确保文件存在于使用以下语句提供的路径中:

file.exists();

检查文件权限是否允许:

file.canExecute(); – return true, file is executable; false is not.
file.canWrite(); – return true, file is writable; false is not.
file.canRead(); – return true, file is readable; false is not.

设置文件权限:

file.setExecutable(boolean); – true, allow execute operations; false to disallow it.
file.setReadable(boolean); – true, allow read operations; false to disallow it.
file.setWritable(boolean); – true, allow write operations; false to disallow it.

关于java - 打开一个存在的文件时出现 FileNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16937030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com