我想刪除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);
}
}
}
現在我設置 scanning path:
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中設置了創建和刪除文件權限,那如何刪除sdcard中特定路徑的所有的文件?
android.permission.WRITE_EXTERNAL_STORAGE 再加上這個試試