【发布时间】:2017-02-06 20:30:21
【问题描述】:
我是 android 编程新手,我正在通过 android 的内部存储进行编码。
我已经使用下面的代码向它写入了文件
File mydir = getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists()) {
mydir.mkdirs();
}
//Getting a file within the dir.
try {
for (int i = 0; i < 100; i++) {
File fileWithinMyDir = new File(mydir, "myfile" + i);
outputStream = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
文件夹是在
下创建的现在我想删除“用户”文件夹及其除一个文件之外的所有内容。假设我想删除所有内容,但名为“文件 56”的文件除外。 我用来删除整个用户文件夹的代码是,
public void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteRecursive(child);
}
}
fileOrDirectory.delete();
}
现在如何删除除该特定文件之外的所有内容?
【问题讨论】: