gpt4 book ai didi

java - Android - 打开刚刚创建的文件时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:29 29 4
gpt4 key购买 nike

我是 android 开发的新手,正在尝试做一些文件 IO。每当我运行这段代码时:

File meta = new File(context.getAppContext().getFilesDir(),"meta");
meta.mkdirs();
File dir = new File(meta,"subdir");
File imageFile = new File(dir,"filename");
Log.d("test",imageFile.getAbsolutePath());
FileOutputStream outputStream = new FileOutputStream(imageFile);

我收到这个错误:

    java.io.FileNotFoundException: /data/data/com.example.android.networkusage/files/meta/Greg and The Morning Buzz/artwork30.jpg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:406)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
at com.example.android.networkusage.Podcast.downloadArtworkFromUrl(Podcast.java:117)
at com.example.android.networkusage.Podcast.<init>(Podcast.java:93)
at com.example.android.networkusage.JSONParser.parse(JSONParser.java:113)
at com.example.android.networkusage.NetworkActivity.loadXmlFromNetwork(NetworkActivity.java:240)
at com.example.android.networkusage.NetworkActivity.access$100(NetworkActivity.java:65)
at com.example.android.networkusage.NetworkActivity$DownloadXmlTask.doInBackground(NetworkActivity.java:203)
at com.example.android.networkusage.NetworkActivity$DownloadXmlTask.doInBackground(NetworkActivity.java:198)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:390)
... 16 more

日志甚至按照指示打印出文件的路径,因此该文件必须存在!为什么会这样?

此外,我的应用程序具有内部和外部写入权限。

最佳答案

其中一个子目录 (subdir) 和文件不存在。该代码应调用 createNewFile() 来实际创建文件。此外,subdir 目录需要通过调用 mkdirs() 来创建。

File meta = new File(context.getAppContext().getFilesDir(),"meta")
meta.mkdirs();
File dir = new File(meta, "subdir");
dir.mkdirs(); //added
File imageFile = new File(dir, "filename.txt");
imageFile.createNewFile(); //added
FileOutputStream outputStream = new FileOutputStream(imageFile);

我不相信 File 构造函数实际上会在文件系统上创建文件。文档指出:

Instances of this class may or may not denote an actual file-system object such as a file or a directory. If it does denote such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname.

关于java - Android - 打开刚刚创建的文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18552948/

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