2012-01-19 108 views
1

我正在嘗試在android中將文件寫入外部存儲。我不斷收到錯誤,「java.io.FileNotFoundException:/mnt/sdcard/Survey.txt(是一個目錄)」。包含此代碼的方法如下:寫入外部存儲filenotfoundexception

public String sendFile(System system) throws FileNotFoundException { 

    File writeable = new File(Environment.getExternalStorageDirectory() 
      .getAbsolutePath() + "/Survey.txt"); 
    try { 
     writeable.createNewFile(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    try { 

     Log.w("Before FO", "***"); 
     if (!Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED)) { 
      Toast.makeText(context, "External SD card not mounted", 
        Toast.LENGTH_LONG).show(); 
     } else { 
      FileOutputStream f = new FileOutputStream(writeable); 
      Log.w("before write", "0"); 

      f.write("HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO".getBytes()); 
      Log.w("before write", "1"); 
      f.close(); 
     } 
    } catch (Exception e) { 
     Log.w("Write sd", e.toString()); 
    } 

    return writeable.getAbsolutePath(); 
} 

我相信程序在FileOutputStream中減速崩潰,因爲Log.w(「FO之前」,「*」);執行。我一直在尋找這個問題的答案几天,所以任何幫助將不勝感激。謝謝!

01-19 14:07:33.839: W/Before FO(641): *** 
01-19 14:07:33.857: W/Write sd(641): java.io.FileNotFoundException: /mnt/sdcard/Survey.txt (Is a directory) 
01-19 14:07:33.857: W/Write sd(641): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 
01-19 14:07:33.857: W/Write sd(641): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239) 
01-19 14:07:33.857: W/Write sd(641): at java.io.FileOutputStream.<init>(FileOutputStream.java:101) 
01-19 14:07:33.857: W/Write sd(641): at java.io.FileOutputStream.<init>(FileOutputStream.java:77) 
01-19 14:07:33.857: W/Write sd(641): at premise.survey.IOInterface.sendFile(IOInterface.java:315) 
01-19 14:07:33.857: W/Write sd(641): at premise.survey.PremiseSurveyActivity.onClick(PremiseSurveyActivity.java:1119) 
01-19 14:07:33.857: W/Write sd(641): at android.view.View.performClick(View.java:3110) 
01-19 14:07:33.857: W/Write sd(641): at android.view.View$PerformClick.run(View.java:11934) 
01-19 14:07:33.857: W/Write sd(641): at android.os.Handler.handleCallback(Handler.java:587) 
01-19 14:07:33.857: W/Write sd(641): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-19 14:07:33.857: W/Write sd(641): at android.os.Looper.loop(Looper.java:132) 
01-19 14:07:33.857: W/Write sd(641): at android.app.ActivityThread.main(ActivityThread.java:4123) 
01-19 14:07:33.857: W/Write sd(641): at java.lang.reflect.Method.invokeNative(Native Method) 
01-19 14:07:33.857: W/Write sd(641): at java.lang.reflect.Method.invoke(Method.java:491) 
01-19 14:07:33.857: W/Write sd(641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
01-19 14:07:33.857: W/Write sd(641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
01-19 14:07:33.857: W/Write sd(641): at dalvik.system.NativeStart.main(Native Method) 
01-19 14:07:33.857: I/ActivityManager(73): Starting: Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras) } from pid 641 
01-19 14:07:33.857: W/WindowManager(73): Failure taking screenshot for (230x341) to layer 21020 
01-19 14:07:34.027: I/ActivityManager(73): Starting: Intent { act=android.intent.action.SEND typ=plain/text flg=0x3000000 cmp=com.android.email/.activity.MessageCompose (has extras) } from pid 641 
01-19 14:07:35.269: I/ActivityManager(73): Displayed com.android.email/.activity.MessageCompose: +1s197ms (total +1s391ms) 
01-19 14:07:40.647: D/dalvikvm(252): GC_EXPLICIT freed 164K, 5% free 7462K/7815K, paused 4ms+3ms 
01-19 14:07:45.727: D/dalvikvm(269): GC_EXPLICIT freed 8K, 8% free 6305K/6791K, paused 13ms+5ms 
01-19 14:12:04.720: D/SntpClient(73): request time failed: java.net.SocketException: Address family not supported by protocol 
01-19 14:12:15.947: I/Email(293): ReconcilePopImapAccountsSync: start 
01-19 14:12:15.977: I/Email(293): ReconcilePopImapAccountsSync: done 
01-19 14:12:16.137: D/dalvikvm(293): GC_CONCURRENT freed 236K, 8% free 7787K/8391K, paused 4ms+7ms 
01-19 14:12:16.137: W/CursorWrapperInner(293): Cursor finalized without prior close() 


Line 315 in IOInterface.java is FileOutputStream f = new FileOutputStream(writeable); 
+1

請登錄您的異常使用不同的代碼:'Log.w(「Write sd」,e);'(no'toString()'for the exception)並從logcat發佈堆棧跟蹤。另外,您可能想要使用以下語法創建File對象:'File writeable = new File(Environment.getExternalStorageDirectory(),「Survey.txt」);' –

回答

2

做了幾個小時的研究這個主題後,我找了個地方被其他人貼過這樣做的不同的方式,它是如下:

String fileName = surveyName + ".csv"; 
String headings = "Hello, world!"; 
File path = Environment.getExternalStorageDirectory(); 
File file = new File(path, fileName); 
path.mkdirs(); 
OutputStream os = new FileOutputStream(file); 
os.write(headings.getBytes()); 
1

您可能會發現您嘗試寫入的文件實際上是一個目錄。找到文件/目錄並刪除它。

我在第一次使用文件時遇到了這個問題。我調用File.mkdirs()期望它創建目錄,但它創建了一個具有我指定的文件名的目錄。

當我創建文件時,我使用兩個File對象。第一個引用就是路徑,我稱之爲mkdirs()。然後第二個引用文件。

//create path 
File directoryFile = new File(Environment.getExternalStorageDirectory(), ""); 
directoryFile.mkdirs(); 

//create file 
File outFile = new File(Environment.getExternalStorageDirectory(), filename);