2 clcurie 11 clcurie_11 于 2013.05.10 10:35 提问

如何删除sdcard中特定路径的所有的文件?

我想删除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中特定路径的所有的文件?

1个回答

u010674064
u010674064   2013.05.13 11:00
已采纳

android.permission.WRITE_EXTERNAL_STORAGE 再加上这个试试

u010674064
u010674064 有用
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片