gpt4 book ai didi

java - 安卓打开失败: ENOTDIR (Not a directory)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:55:26 28 4
gpt4 key购买 nike

我尝试在内部驱动器上生成一些文件时遇到此错误。下面是打印栈

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);
}

最佳答案

尝试检查文件夹是否存在。
例如)

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.
}

关于java - 安卓打开失败: ENOTDIR (Not a directory),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676667/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com