简体   繁体   English

为什么我不能在内部目录中创建文件?

[英]Why I can't create a file in the internal directory?

I'm trying to create a new file by: 我正在尝试通过以下方式创建新文件:

File file = new File(this.getFilesDir(), "test.tmp");

After that I have the follwing code: 之后,我有以下代码:

try {
      fis = new BufferedInputStream(new FileInputStream(file));
      ObjectInputStream ois = new ObjectInputStream(fis);

      ..... other not important code

And it throws the following error: 并引发以下错误:

android.system.ErrnoException: open failed: ENOENT (No such file or directory)

regarding this line: 关于这条线:

fis = new BufferedInputStream(new FileInputStream(file));

Isn't File file = new File(this.getFilesDir(), "test.tmp"); 不是File file = new File(this.getFilesDir(), "test.tmp"); supposed to create the file? 应该创建文件?

Can you give me a clue? 能给我个提示吗? I know that I'm missing a small but important part here, but I'm unable to spot it. 我知道我在这里缺少一小部分但很重要的部分,但是我无法发现它。

Isn't File file = new File(this.getFilesDir(), "test.tmp"); 不是File file = new File(this.getFilesDir(),“ test.tmp”); supposed to create the file? 应该创建文件?

No. It creates a Java object, an instance of class File . 不。它创建一个Java对象,即File类的实例。 It has no impact on the filesystem. 它对文件系统没有影响。

Use exists() on the File object to see if the file exists. File对象上使用exists() File是否存在。 Or, if for some reason you want the empty file there, call createNewFile() on the File object. 或者,由于某种原因,如果您希望在那里有空文件,请在File对象上调用createNewFile()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM