如何找到空sub_folder并使用android删除空sub_folder?
How to find the empty sub_folder and delete the empty sub_folder using android?
我是否正在使用 android 使用文件概念创建示例应用程序,
以下步骤是:
--- 1.我给父文件夹取名"default"
--- 2. 找到父文件夹到子文件夹。
--- 3.列出子文件夹文件。
--- 4.删除清空子文件夹
我会完成三个步骤,问题是如何找到子文件夹是空的给我一个解决方案?
示例代码:
File filefirst = new File("/storage/sdcard0/Parentfoldername/");
String[] names = filefirst.list();
for (String name : names)
{
if (new File("/storage/sdcard0/Parentfoldername/" + name).isDirectory()) {
File directory = new File("/storage/sdcard0/Parentfoldername/" + name);
//get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile())
{
if (directory.isDirectory())
{
String[] children = directory.list();
for (int i = 0; i < children.length; i++)
{
new File(directory, children[i]).delete();
}
}
}
}
}
}
此代码类似于删除文件中的 sub_folder,删除 sub_folder 不是文件夹是文件,但 Empty sub_folder 不是删除。
public class Utils {
/**
* Empty and delete a folder (and subfolders).
* @param folder
* folder to empty
*/
public static void rmdir(final File folder) {
// check if folder file is a real folder
if (folder.isDirectory()) {
File[] list = folder.listFiles();
if (list != null) {
for (int i = 0; i < list.length; i++) {
File tmpF = list[i];
if (tmpF.isDirectory()) {
rmdir(tmpF);
}
tmpF.delete();
}
}
if (!folder.delete()) {
System.out.println("can't delete folder : " + folder);
}
}
}
}
我是否正在使用 android 使用文件概念创建示例应用程序,
以下步骤是:
--- 1.我给父文件夹取名"default"
--- 2. 找到父文件夹到子文件夹。
--- 3.列出子文件夹文件。
--- 4.删除清空子文件夹
我会完成三个步骤,问题是如何找到子文件夹是空的给我一个解决方案?
示例代码:
File filefirst = new File("/storage/sdcard0/Parentfoldername/");
String[] names = filefirst.list();
for (String name : names)
{
if (new File("/storage/sdcard0/Parentfoldername/" + name).isDirectory()) {
File directory = new File("/storage/sdcard0/Parentfoldername/" + name);
//get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile())
{
if (directory.isDirectory())
{
String[] children = directory.list();
for (int i = 0; i < children.length; i++)
{
new File(directory, children[i]).delete();
}
}
}
}
}
}
此代码类似于删除文件中的 sub_folder,删除 sub_folder 不是文件夹是文件,但 Empty sub_folder 不是删除。
public class Utils {
/**
* Empty and delete a folder (and subfolders).
* @param folder
* folder to empty
*/
public static void rmdir(final File folder) {
// check if folder file is a real folder
if (folder.isDirectory()) {
File[] list = folder.listFiles();
if (list != null) {
for (int i = 0; i < list.length; i++) {
File tmpF = list[i];
if (tmpF.isDirectory()) {
rmdir(tmpF);
}
tmpF.delete();
}
}
if (!folder.delete()) {
System.out.println("can't delete folder : " + folder);
}
}
}
}