2012-07-23 92 views
52

我正在尝试使用整数数组保存一些数据块覆盖范围,这些数据只是简单地保存了一个数据块的执行次数。但是,由于某些原因,当我尝试写入某些创建的文件(例如“BlockForHelper.txt”,例如我在Eclipse中制作并放置在项目目录中)时,出现此错误:Android错误 - 打开失败ENOENT

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

任何人都知道可能会导致什么呢?

+0

eclipse中的什么文件夹你把这个txt文件? – 2012-07-23 21:21:30

+0

只是我的项目的根目录。为什么? – NioShobu 2012-07-23 21:25:10

+0

我有这个问题。我删除了以编程方式创建的文件夹,并手动创建并解决了问题! – breceivemail 2013-08-24 08:30:57

回答

63

使用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"); 
+0

那么我应该在哪里写这个?我可以把它扔进资产并写在那里吗?我之前用数据库做过。我的主要问题是我希望能够稍后阅读。 – NioShobu 2012-07-23 21:32:52

+0

尝试File file = new File(“/ data/data/your.package.name/nfs/guille/groce/users/nicholsk/workspace3/SQLTest/BlockForTest.txt”); – SteveR 2012-07-23 21:41:02

+2

比我以前的评论更好:'File file = new File(context.getFilesDir(),“yourFolder”);' – SteveR 2012-07-23 23:11:56

3

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

+0

你能给我一个这样的代码的例子吗? – NioShobu 2012-07-23 22:26:04

+1

我抬头看,发现显然你不能写任何资产。 – NioShobu 2012-07-23 22:55:22

相关问题