我正在尝试通过以下方式创建一个新文件:
文件 file = new File(this.getFilesDir(), "test.tmp");
之后我有以下代码:
try {
fis = new BufferedInputStream(new FileInputStream(file));
ObjectInputStream ois = new ObjectInputStream(fis);
..... other not important code
它会抛出以下错误:
android.system.ErrnoException:打开失败:ENOENT(没有这样的文件或目录)
关于这一行:
fis = new BufferedInputStream(new FileInputStream(file));
File file = new File(this.getFilesDir(), "test.tmp");
不应该创建文件吗?
你能给我一个线索吗?我知道我在这里遗漏了一个小但重要的部分,但我无法发现它。
最佳答案
Isn't File file = new File(this.getFilesDir(), "test.tmp"); supposed to create the file?
没有。它创建一个 Java 对象,即 File
类的实例。它对文件系统没有影响。
在File
对象上使用exists()
来查看文件是否存在。或者,如果出于某种原因您想要空文件,请在 File
对象上调用 createNewFile()
。
关于java - 为什么我无法在内部目录中创建文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28564640/