我想删除 sdcard 特定路径中的所有文件。我使用下面的代码:
private void readFile(File[] file) {
for (int i = 0; file != null && i < file.length; i++) {
// now i check if it is a file or filename extension
if (file[i].isFile() && file[i].getName().endsWith("txt")) {
fileList.add(file[i].toString());
} else if (file[i].isDirectory()) {// if it is a file,recursive scanning
File[] newFileList = new File(file[i].getAbsolutePath()).listFiles();
readFile(newFileList);
}
}
}
现在我设置扫描路径:
public void onStart(Intent intent, int startId) {
String res = "";
try {
fileList = new ArrayList<String>();
File[] files = new File(FILEPATH).listFiles();
readFile(files);
for (File file : files) {
Log.i("syso", "" + file);
FileInputStream fin = new FileInputStream(file);
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
// System.out.println(res);
boolean isDelete = file.delete();
if (isDelete = true) {
System.out.println("Deleted successful!!");
} else {
System.out.println("Deleted fail!!");
}
我在 SDCard 中设置了创建和删除文件权限