2013-04-24 54 views
0

我有以下代碼:Android:FileNotFoundException如何創建文件?

try{ 
      File sdCard = Environment.getExternalStorageDirectory(); 
      File directory = new File (sdCard.getAbsolutePath() + "/statReporter/"); 
      directory.mkdirs(); 

      //Path and name of XML file 
      File file = new File(directory, appendTimeStamp("statReporter") + ".csv"); 
      FileOutputStream fOut = new FileOutputStream(file); 
      OutputStreamWriter osw = new OutputStreamWriter(fOut); 

      //Write to CSV File 
      osw.write(message); 
      osw.write(','); 

      //Flush and close OutPutStreamWriter and close FileOutputStream 
      osw.flush(); 
      osw.close(); 
      fOut.close(); 

      Log.d("File Logger:", "File write successfully!"); 

    }catch(IOException ioe){ 
      ioe.printStackTrace(); 
    } 

我得到以下異常:

java.io.FileNotFoundException: /mnt/sdcard/statReporter/Date:3/24/2013Time:11:932statReporter,.csv: open failed: ENOENT (No such file or directory) 

回答

0

我發現了錯誤:

我改變文件名。刪除「:」和「/」和「,」

0

在文件名Date:3/24/2013Time:11:932statReporter,.csv/分隔符..這將被視爲一個目錄。