private void readFile(File[] file) {
for (int i = 0; file != null && i < file.length; i++) {
// 判读是否文件以及文件后缀名
if (file[i].isFile() && file[i].getName().endsWith("txt")) {
fileList.add(file[i].toString());
} else if (file[i].isDirectory()) {// 如果是文件夹,递归扫描
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("删除成功!!");
} else {
System.out.println("删除失败!!");
}
fin.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// <!-- 在SDCard中创建与删除文件权限 -->
// <uses-permission
// android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
// 权限我也加了
我记得是write什么sdcard来着。搞错了吧
* * 递归删除目录下的所有文件及子目录下所有文件 *
* @param dir 将要删除的文件目录 *
* @return boolean Returns
* "true" if all deletions were successful. * If a deletion fails, the
* method stops attempting to * delete and returns "false".
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
// 递归删除目录中的子目录下
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
} // 目录此时为空,可以删除
return dir.delete();
}
private String getAllPath(String dirpath) {
File file = new File(dirpath);
String path;
if (file.exists()) {
if (file.isDirectory()) {
File[] tmp = file.listFiles();
if(tmp!=null){
for (File f : tmp) {
getAllPath(f.getAbsolutePath());
}
}
} else {
path = file.getPath();
allFilePath.add(path);
return path;
}
}
return null;
}参照此方法 应该不需要用流来删除吧~~