File.delete有些文件不能删除,而Files.delete(path)可以,进行深入探究其原因

前言

最近更换deepin系统,在其上做项目,发现运行Java程序,File.delete有的目录下的文件,不能删除,有的能删除,反复对比文件属性及操作权限,均不得其解,最后发现采用Paths和Files得到解决!

我的问题

package com.stati.test;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;

import ch.qos.logback.core.util.FileUtil;

public class TestFile {

	public static void main(String[] args) throws IOException, InterruptedException {
		File file = new File("/home/tmpuser/tmp/test1/"); // 获取其file对象
		File[] files = file.listFiles(); // 遍历path下的文件和目录,放在File数组中
		for (File f : files) { // 遍历File[]数组
	
			  boolean flag = FileUtils.deleteQuietly(f); 
			  System.out.println("------------------------"+flag); 
			  if (f.delete()) // 若非目录(即文件),则打印 
				  System.out.println("---删除文件成功----" + f.getAbsolutePath()); 
			  if (f.exists()) { 
				  // System.gc(); 
				  Process process = Runtime.getRuntime().exec("rm -rf "+f.getAbsolutePath()); 
				  int exitValue = process.waitFor(); 
				  if (0 != exitValue) { 
					  System.out.println("删除文件失败---" + f.getAbsolutePath()); 
					  } else { 
						  System.out.println("删除文件成功---" + f.getAbsolutePath()); 
						  } 
				  // System.out.println("---删除文件成功----" + f.getAbsolutePath()); 
				  }
			 
		}

	}

}

以上代码捣鼓好久,硬是对/home/tmpuser/tmp/test1下的文件,删除不掉,无论我是重建目录,更改权限,都是失败!

以上代码我我采用至少三种方法:

1、FileUtils.deleteQuietly(f)

2、f.delete()

3、Runtime.getRuntime().exec("rm -rf "+f.getAbsolutePath()); 

均没有起作用,当时在网上,不断搜寻解决方案,反复都是一下三种:

1、多线程操作;
2、打开的文件流没有关闭;
3、需要垃圾回收下System.gc(); 。

我也反复核对我的项目代码,反复尝试,均不行!深入DEBUG代码,发现security == null

百思不得其解,说我的系统不安全(我的是deepin系统,大概1周没有重启电脑了),但是有一个目录可以删除……暂停!但是我想到jdk8中的Files、Path,居然解决了我的问题!

删除文件用jdk8的Files.delete(path)

package com.stati.test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stre
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Speed up the execution of important database queries by making good choices about which indexes to create. Choose correct index types for different scenarios. Avoid indexing pitfalls that can actually have indexes hurting performance rather than helping. Maintain indexes so as to provide consistent and predictable query response over the lifetime of an application. Expert Oracle Indexing and Access Paths is about the one database structure at the heart of almost all performance concerns: the index. Database system performance is one of the top concerns in information technology today. Administrators struggle to keep up with the explosion of access and activity driven by the proliferation of computing into everything from phones to tablets to PCs in our increasingly connected world. At the heart of any good-performing database lies a sound indexing strategy that makes appropriate use of indexing, and especially of the vendor-specific indexing features on offer. Few databases fully exploit the wealth of data access mechanisms provided by Oracle. Expert Oracle Indexing and Access Paths helps by bringing together information on indexing and how to use it into one blissfully short volume that you can read quickly and have at your fingertips for reference. Learn the different types of indexes available and when each is best applied. Recognize when queries aren’t using indexes as you intend. Manage your indexing for maximum performance. Confidently use the In Memory column store feature as an alternate access path to improve performance. Let Expert Indexing in Oracle Database 12c be your guide to deep mastery of the most fundamental performance optimization structure in Oracle Database. Explains how indexes help performance, and sometimes hinder it too Demystifies the various index choices so that you can chose rightly Describes the database administration chores associated with indexes Demonstrates the use of the In Memory column store as an alternate access path to the data What You Will Learn Create an overall indexing strategy to guide your decisions Choose the correct indexing mechanisms for your applications Manage and maintain indices to avoid degradation and preserve efficiency Take better advantage of underused index types such as index-organized tables Choose the appropriate columns to index, with confidence Blend partitioning and materialized views into your indexing strategy Who This Book Is For Expert Oracle Indexing and Access Paths is for all levels of database administrators and application developers who are struggling with the database performance and scalability challenge. Any database administrator involved with indexing, which is any database administrator period, will appreciate the wealth of advice packed into this gem of a book. Table of Contents Chapter 1: Introduction to Oracle Indexes Chapter 2: B-tree Indexes Chapter 3: Bitmap Indexes Chapter 4: Index-Organized Tables Chapter 5: Specialized Indexes Chapter 6: Partitioned Indexes Chapter 7: Tuning Index Usage Chapter 8: Maintaining Indexes Chapter 9: SQL Tuning Advisor Chapter 10: In-Memory Column Store

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿啄debugIT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值