简体   繁体   English

Android - 刚刚创建的错误打开文件

[英]Android - error opening file just created

I'm new to android developement and trying to do some file IO. 我是android developpement的新手,并尝试做一些文件IO。 Whenever I run this block of code: 每当我运行这段代码时:

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);

I get this error: 我收到此错误:

    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

The log even prints out the file's path as directed, so the file must exist! 日志甚至按照指示打印出文件的路径,因此文件必须存在! Why is this happening? 为什么会这样?

Also, my app has internal and external write priviledges. 此外,我的应用程序具有内部和外部写入权限。

One of the sub-directories ( subdir ) and file do not exist. 其中一个子目录( subdir )和文件不存在。 The code should call createNewFile() to actually create the file. 代码应该调用createNewFile()来实际创建文件。 Also the subdir directory needs to be made by calling mkdirs() . 还需要通过调用mkdirs()来生成subdir目录。

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);

I do not believe the File constructor actually creates the file on the file system. 我不相信File构造函数实际上在文件系统上创建文件。 The documentation states: 文件说明:

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 (eg a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. 单个存储设备(例如,物理磁盘驱动器,闪存,CD-ROM)可以包含多个分区。 The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname. 该对象(如果有)将驻留在由此路径名的绝对形式的某个祖先命名的分区上。

this is a snippet of code from a project i'm working on right now : 这是我正在进行的项目的一小段代码:

from inside an activity/context : 来自活动/背景:

File dir = getDir("Foler", 0);        // i think your problem is here
File file = new File(dir,"File.bin");

creating directory in android is different than java on normal computer, try to create your directory using getDir() instead of new File(...).mkdirs(), and make sure it is from inside a context (after the activity is created) 在android中创建目录与普通计算机上的java不同,尝试使用getDir()而不是新文件(...)。mkdirs()创建目录,并确保它来自上下文(在创建活动之后) )

i dont believe i'm the best one to answer this question since i'm new to android as well, but maybe this helps 我不相信我是回答这个问题的最佳人选,因为我也是Android的新手,但也许这会有所帮助

so final code maybe like this 所以最终的代码可能是这样的

File dir = getDir("subdir",0);
File imageFile = new File(dir, "filename.txt");
imageFile.createNewFile();
FileOutputStream outputStream = new FileOutputStream(imageFile);

i dont know about making nested directories, i didnt try it up till now 我不知道制作嵌套目录,我直到现在都没有尝试过

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

相关问题 打开使用java创建的zip文件时出错 - Error opening zip file created using java 在android中打开pdf文件时出错, - error in opening pdf file in android, Android - 错误打开跟踪文件:没有这样的文件或目录 - Android - error opening trace file: No such file or directory 打开使用Apache POI创建的邮件附加excel文件时出错 - Error in opening an mail attached excel file created using Apache POI 从我刚刚创建的文件中获取Uri(android) - Get Uri from file I just created (android) 错误打开跟踪文件android权限被拒绝 - Error opening trace file android permission denied Android运行时错误:打开跟踪文件时出错:没有这样的文件或目录(2) - Android runtime error: error opening trace file: No such file or directory (2) Android E / Trace(627):打开跟踪文件时出错:没有这样的文件或目录(2) - Android E/Trace(627): error opening trace file: No such file or directory (2) Android上的JDom:打开跟踪文件时出错:没有这样的文件或目录 - JDom on Android: error opening trace file: No such file or directory 通过eclipse打开Java文件没有错误,但是如果我只是复制源代码,则会产生错误 - No-error on opening a java file through eclipse, but produces errors if I just copy the source code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM