2012-07-23 91 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

相關問題