android如何创建和删除文件夹,网上说的各种方法都试过了
下面是代码
//创建文件夹?????????????????????????????????????????
void createFile(File onClickFile, String fileName) {
File f = new File(onClickFile.getAbsolutePath() + File.separator + fileName + File.separator);
if (f.exists()) {
Toast.makeText(listActivity, "文件夹已存在", Toast.LENGTH_SHORT).show();
} else {
boolean bol = f.mkdir();
System.out.println("f.getAbsolutePath()---------" + f.getAbsolutePath());
if (bol) {
Toast.makeText(listActivity, "文件夹创建成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(listActivity, "文件夹创建失败1", Toast.LENGTH_SHORT).show();
}
}
}
//删除文件???????????????????????????????????????????
void deleteFile(File f) {
System.out.println("f.getAbsolutePath()-----------" + f.getAbsolutePath());
System.out.println("f.exists()-----------" + f.exists());
System.out.println("f.canRead()-----------" + f.canRead());
System.out.println("f.canWrite()-----------" + f.canWrite());
System.out.println("f.isDirectory()-----------" + f.isDirectory());
if (f.isFile()) {
f.delete();
} else if (f.isDirectory()) {
File[] files = f.listFiles();
if (files.length == 0) {
f.delete();
} else {
for (int i = 0; i < files.length; i++) {
deleteFile(files[i]);
}
}
}
//重新遍历父目录
clickItem(parentFile);
if (!f.exists()) {
Toast.makeText(listActivity, "文件删除成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(listActivity, "文件删除失败", Toast.LENGTH_SHORT).show();
}
}