Android:FileNotFoundException如何创建文件?

时间:2013-04-24 17:15:18

标签: 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)

2 个答案:

答案 0 :(得分:0)

我发现了错误:

我更改了文件名。摆脱“:”和“/”和“,”

答案 1 :(得分:0)

在文件名Date:3/24/2013Time:11:932statReporter,.csv中有/分隔符......它将被视为目录。