【问题标题】:Android Error - Open Failed ENOENTAndroid 错误 - 打开失败 ENOENT
【发布时间】:2023-03-04 22:59:01
【问题描述】:

我正在尝试使用一个整数数组来保存一些块覆盖率,该数组只是保存一个块被执行的次数。但是,由于某种原因,当我尝试写入我创建的一些文件(例如“BlockForHelper.txt”,我专门在 Eclipse 中创建并放置在项目目录中)时,我收到了这个错误:

java.io.FileNotFoundException:  /nfs/guille/groce/users/nicholsk/workspace3/SQLTest/BlockForTest: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
at com.example.sql2.SQLTest.blockCoverage(SQLTest.java:149)
at com.example.sql2.test.SQLTestCase.testSuite(SQLTestCase.java:41)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
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:400)
... 18 more

并给我错误:

public void blockCoverage() throws IOException
{
    String coverage = "";
    for (int x = 0; x < 20; x++)
        coverage += x + " " + bb_count[x] + "\n";

    File file = new File("/nfs/guille/groce/users/nicholsk/workspace3/SQLTest/BlockForTest.txt");
    Writer out = new OutputStreamWriter(new FileOutputStream(file)); // Here
    try
    {
        out.write(coverage);
    } finally {
        out.close();
    }
}

有人知道这可能是什么原因吗?

【问题讨论】:

  • 你把这个txt文件放在eclipse的哪个文件夹里了?
  • 只是我项目的根目录。为什么?
  • 我遇到了这个问题。我删除了以编程方式创建的文件夹并手动创建并解决了问题!!!

标签: android eclipse fileoutputstream


【解决方案1】:

使用 sdk,您无法写入内部存储的根目录。这会导致您的错误。

编辑:

根据您的代码,将内部存储与 sdk 一起使用:

final File dir = new File(context.getFilesDir() + "/nfs/guille/groce/users/nicholsk/workspace3/SQLTest");
dir.mkdirs(); //create folders where write files
final File file = new File(dir, "BlockForTest.txt");

【讨论】:

  • 那么我应该把这个写到哪里呢?我可以把它放在资产中并在那里写吗?我之前确实使用数据库做到了这一点。我的主要问题是我希望以后能够阅读它。
  • 尝试文件 file = new File("/data/data/your.package.name/nfs/guille/groce/users/nicholsk/workspace3/SQLTest/BlockForTest.txt");
  • 比我之前的评论更好:File file = new File (context.getFilesDir(), "yourFolder");
  • 我遇到了这个问题。我删除了以编程方式创建的文件夹并手动创建它,问题解决了!!!
【解决方案2】:

将文本文件放在资产目录中。如果没有资产目录,则在项目的根目录中创建一个。然后你可以使用Context.getAssets().open("BlockForTest.txt"); 来打开这个文件的流。

【讨论】:

  • 我查了一下,发现你显然不能写入 assets 中的任何东西。
猜你喜欢
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 2013-02-04
  • 1970-01-01
相关资源
最近更新 更多