2014-03-27 115 views
4

我在嘗試生成某些文件到內部驅動器時出現此錯誤。以下是printstackAndroid打開失敗:ENOTDIR(不是目錄)

03-27 01:36:16.111: W/System.err(1445): java.io.FileNotFoundException: /data/data/dev.shaw.MyShoppingPlanner/files/Stores/Giant_314_Horsham_Rd.txt: open failed: ENOTDIR (Not a directory) 

的代碼,這

private void generateFiles() throws IOException { 

    File storelist = new File(this.getFilesDir() + File.separator +"Stores"); 
    File listnames = new File(this.getFilesDir() + File.separator + "Lists"); 
    listnames.mkdirs(); 
    storelist.mkdirs(); 
    //generate the store list, (Store Name) (street# Street la./rd./ave./etc.) (City/Town State ZipCode) 
    String stores[] ={"Giant Foodstore 314 Horsham Rd. Horsham Pa. 19044", "Acme Food 200 Blair Mill Rd. Horsham Pa. 19044"}; 
    String lnames[] = {"Default List"}; 
    String Dstorepath = storelist+File.separator + "Giant_314_Horsham_Rd.txt"; 
    String Dlistpath = listnames + File.separator + "Default_List.txt"; 
    File sample_store = new File(Dstorepath); 
    File sample_list = new File(Dlistpath); 
    FileOutputStream fos = new FileOutputStream(sample_store); 
    fos.write(stores[0].getBytes()); 
    Toast.makeText(this,"Store File Generated" , Toast.LENGTH_SHORT).show(); 
    fos = new FileOutputStream(sample_list); 
    fos.write(lnames[0].getBytes()); 
    Toast.makeText(this,"List File Generated" , Toast.LENGTH_SHORT).show(); 
    Intent intent = new Intent(this, MainActivity.class); 
    startActivity(intent); 
} 
+0

您在清單中啓用了哪些權限? – stevebot

+0

這是 – user3451418

回答

13

嘗試檢查文件夾是否存在。
ex)

File listnames = new File(this.getFilesDir() + File.separator + "Lists"); 
if(!listnames.exists()) 
    listnames.mkdirs(); 
else if(!listnames.isDirectory() && listnames.canWrite()){ 
    listnames.delete(); 
    listnames.mkdirs(); 
} 
else{ 
    //you can't access there with write permission. 
    //Try other way. 
} 
+0

感謝工作 – user3451418

+0

謝謝你,正是我的問題是關於與我想創建的目錄同名的文件!這些行幫助我:listnames.delete(); listnames.mkdirs(); –

相關問題