data/data/包名/files下的文件,调用file.delete,为什么删不掉

johnyin2015 2020-06-24 10:19:55
设备:AS自带模拟器Pixel 2 API 25,Lenovo TB-X705M Android8.1.0
delete调用了,但返回false,未出现任何异常。
通过chmod授予了文件777dwrx权限。
绝对路径和相对路径都试过。
rm命令可以删除文件。
    public void delFiles(View v) throws IOException {
//Toast.makeText(this,"点击了删除文件按钮",Toast.LENGTH_SHORT).show();
//deleteExpiredFile(0,getFilesDir()+"");
String dir = getExternalFilesDir(null).getAbsolutePath() + File.separator + "tmpxxx";
File d = new File(dir);
if (!d.exists())
d.mkdir();
deleteExpiredFile(0,d);
//deleteDirectory(getFilesDir().getAbsoluteFile());
}

/**
* 删除超过保存时间的日志文件
*
* @param day 保存时间
*/
private static void deleteExpiredFile(long day, File dir) {
long currentTime = System.currentTimeMillis();
//File dir = new File(filePath);
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {

for (File item : files) {
try {
long lastModified = item.lastModified();
if (currentTime - lastModified > day) {
boolean isdelect = item.delete();//删除目录成功
if (isdelect){
Log.d("ff","1111");
}
}



} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

public void deleteDirectory(File file) throws IOException {
if( file.exists() ) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
if (!files[i].delete()){
throw new IOException("failed to the file");
}else{
Log.d("aa","删除成功");
}
}
}
}
//file.delete();
}
}
...全文
142 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
请教大佬 2020-07-17
  • 打赏
  • 举报
回复
你的那个shell执行类可以发给我看看么
请教大佬 2020-07-17
  • 打赏
  • 举报
回复
你的那个shell执行类可以发给我看看么

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧