org.apache.commons.io.FileUtils.deleteQuietly()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(2049)

本文整理了Java中org.apache.commons.io.FileUtils.deleteQuietly()方法的一些代码示例,展示了FileUtils.deleteQuietly()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.deleteQuietly()方法的具体详情如下:
包路径:org.apache.commons.io.FileUtils
类名称:FileUtils
方法名:deleteQuietly

FileUtils.deleteQuietly介绍

[英]Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.

The difference between File.delete() and this method are:

  • A directory to be deleted does not have to be empty.
  • No exceptions are thrown when a file or directory cannot be deleted.
    [中]删除文件,从不引发异常。如果文件是一个目录,请删除它和所有子目录。
    文件之间的差异。delete()和此方法是:
    *要删除的目录不必为空。
    *无法删除文件或目录时不会引发异常。

代码示例

代码示例来源:origin: gocd/gocd

private void cleanupOldPluginDirectories() {
    FileUtils.deleteQuietly(new File("plugins_bundles"));
    FileUtils.deleteQuietly(new File("plugins-new"));
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

private void removeFile( File file ) {
 synchronized ( file ) {
  if ( file.exists() ) {
   FileUtils.deleteQuietly( file );
  }
 }
}

代码示例来源:origin: gocd/gocd

private void reapFiles() {
  try (FileReader tmpFileReader = new FileReader(LAUNCHER_TMP_FILE_LIST)) {
    List<String> fileList = IOUtils.readLines(tmpFileReader);
    Set<String> fileSet = new HashSet<>(fileList);
    for (String fileName : fileSet) {
      File file = new File(fileName);
      FileUtils.deleteQuietly(file);
      File depsDir = new File(FileUtil.TMP_PARENT_DIR, fileName);
      FileUtils.deleteQuietly(depsDir);
      if (!file.exists() && !depsDir.exists()) {
        fileList.remove(fileName);
      }
    }
    writeToFile(fileList, false);
  } catch (Exception ignore) {
  }
}

代码示例来源:origin: apache/incubator-pinot

public void deleteSegmentFile() {
  // If segment build fails with an exception then we will not be able to create a segment file and
  // the file name will be null.
  if (_segmentTarFilePath != null) {
   FileUtils.deleteQuietly(new File(_segmentTarFilePath));
  }
 }
}

代码示例来源:origin: alipay/sofa-jarslink

public void initWorkingDir() {
  String workingDir = EnvironmentUtils.getProperty(Constants.JARSLINK_WORKING_DIR);
  File dirFile = StringUtils.isEmpty(workingDir) ? null : new File(workingDir);
  if (StringUtils.isEmpty(workingDir)) {
    workingDir = FileUtils.getTempDirectoryPath() + File.separator
           + Constants.JARSLINK_IDENTITY;
    dirFile = new File(workingDir);
    FileUtils.deleteQuietly(dirFile);
    dirFile.mkdir();
    dirFile.deleteOnExit();
    EnvironmentUtils.setProperty(Constants.JARSLINK_WORKING_DIR, workingDir);
  }
  AssertUtils.isTrue(dirFile.exists() && dirFile.isDirectory(),
    "Jarslink Working directory must exist.");
}

代码示例来源:origin: gocd/gocd

private void wipePluginBundleDirectory(File pluginBundleDirectory) {
  if (pluginBundleDirectory.exists() && !FileUtils.deleteQuietly(pluginBundleDirectory)) {
    throw new RuntimeException(String.format("Failed to delete bundle directory %s", pluginBundleDirectory));
  }
  pluginBundleDirectory.mkdirs();
}

代码示例来源:origin: alibaba/nacos

static public void clearAll() {
  File file = new File(NACOS_HOME, BASE_DIR);
  if (FileUtils.deleteQuietly(file)) {
    LogUtil.defaultLog.info("clear all config-info success.");
  } else {
    LogUtil.defaultLog.warn("clear all config-info failed.");
  }
  File fileTenant = new File(NACOS_HOME, TENANT_BASE_DIR);
  if (FileUtils.deleteQuietly(fileTenant)) {
    LogUtil.defaultLog.info("clear all config-info-tenant success.");
  } else {
    LogUtil.defaultLog.warn("clear all config-info-tenant failed.");
  }
}

代码示例来源:origin: apache/incubator-pinot

@BeforeMethod
public void setUpTest()
  throws IOException, ConfigurationException {
 segmentDir = new File(TEST_DIRECTORY, "segmentDirectory");
 if (segmentDir.exists()) {
  FileUtils.deleteQuietly(segmentDir);
 }
 segmentDir.mkdirs();
 segmentDir.deleteOnExit();
 writeMetadata();
}

代码示例来源:origin: apache/incubator-pinot

/**
 * Deletes the destination file if it exists then calls org.apache.commons moveFile.
 * @param srcFile
 * @param destFile
 */
public static void moveFileWithOverwrite(File srcFile, File destFile)
  throws IOException {
 if (destFile.exists()) {
  org.apache.commons.io.FileUtils.deleteQuietly(destFile);
 }
 org.apache.commons.io.FileUtils.moveFile(srcFile, destFile);
}

代码示例来源:origin: alibaba/nacos

static public void clearAllBeta() {
  File file = new File(NACOS_HOME, BETA_DIR);
  if (FileUtils.deleteQuietly(file)) {
    LogUtil.defaultLog.info("clear all config-info-beta success.");
  } else {
    LogUtil.defaultLog.warn("clear all config-info-beta failed.");
  }
  File fileTenant = new File(NACOS_HOME, TENANT_BETA_DIR);
  if (FileUtils.deleteQuietly(fileTenant)) {
    LogUtil.defaultLog.info("clear all config-info-beta-tenant success.");
  } else {
    LogUtil.defaultLog.warn("clear all config-info-beta-tenant failed.");
  }
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testDeleteQuietlyNonExistent() {
  final File testFile = new File("testDeleteQuietlyNonExistent");
  assertFalse(testFile.exists());
  try {
    FileUtils.deleteQuietly(testFile);
  } catch (final Exception ex) {
    fail(ex.getMessage());
  }
}

代码示例来源:origin: commons-io/commons-io

/**
 * Initialise the wrapped file writer.
 * Ensure that a cleanup occurs if the writer creation fails.
 *
 * @param file  the file to be accessed
 * @param encoding  the encoding to use
 * @param append  true to append
 * @return The initialised writer
 * @throws IOException if an error occurs
 */
private Writer initWriter(final File file, final Charset encoding, final boolean append) throws IOException {
  final boolean fileExistedAlready = file.exists();
  try {
    return new OutputStreamWriter(new FileOutputStream(file.getAbsolutePath(), append),
                   Charsets.toCharset(encoding));
  } catch (final IOException | RuntimeException ex) {
    FileUtils.deleteQuietly(lockFile);
    if (fileExistedAlready == false) {
      FileUtils.deleteQuietly(file);
    }
    throw ex;
  }
}

代码示例来源:origin: alibaba/nacos

static public void clearAllTag() {
    File file = new File(NACOS_HOME, TAG_DIR);
    if (FileUtils.deleteQuietly(file)) {
      LogUtil.defaultLog.info("clear all config-info-tag success.");
    } else {
      LogUtil.defaultLog.warn("clear all config-info-tag failed.");
    }
    File fileTenant = new File(NACOS_HOME, TENANT_TAG_DIR);
    if (FileUtils.deleteQuietly(fileTenant)) {
      LogUtil.defaultLog.info("clear all config-info-tag-tenant success.");
    } else {
      LogUtil.defaultLog.warn("clear all config-info-tag-tenant failed.");
    }
  }
}

代码示例来源:origin: apache/incubator-pinot

@BeforeMethod
public void setUpTest()
  throws IOException, ConfigurationException {
 segmentDir = new File(TEST_DIRECTORY, "segmentDirectory");
 if (segmentDir.exists()) {
  FileUtils.deleteQuietly(segmentDir);
 }
 if (segmentDir.exists()) {
  throw new RuntimeException("directory exists");
 }
 segmentDir.mkdirs();
 segmentMetadata = ColumnIndexDirectoryTestHelper.writeMetadata(SegmentVersion.v1);
}

代码示例来源:origin: apache/incubator-pinot

private void deleteV2Files(File v2SegmentDirectory) {
 LOGGER.info("Deleting files in v1 segment directory: {}", v2SegmentDirectory);
 File[] files = v2SegmentDirectory.listFiles();
 if (files == null) {
  // unexpected condition but we don't want to stop server
  LOGGER.error("v1 segment directory: {}  returned null list of files", v2SegmentDirectory);
  return;
 }
 for (File file : files) {
  if (file.isFile() && file.exists()) {
   FileUtils.deleteQuietly(file);
  }
 }
}

代码示例来源:origin: apache/incubator-pinot

@Override
public void cleanup(Context context)
  throws IOException, InterruptedException {
 File currentDiskWorkDir = new File(_currentDiskWorkDir);
 LOGGER.info("Clean up directory: {}", currentDiskWorkDir.getAbsolutePath());
 FileUtils.deleteQuietly(currentDiskWorkDir);
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testDeleteQuietlyFile() throws IOException {
  final File testFile = new File(getTestDirectory(), "testDeleteQuietlyFile");
  if (!testFile.getParentFile().exists()) {
    throw new IOException("Cannot create file " + testFile
        + " as the parent directory does not exist");
  }
  final BufferedOutputStream output =
      new BufferedOutputStream(new FileOutputStream(testFile));
  try {
    TestUtils.generateTestData(output, 0);
  } finally {
    IOUtils.closeQuietly(output);
  }
  assertTrue(testFile.exists());
  FileUtils.deleteQuietly(testFile);
  assertFalse("Check No Exist", testFile.exists());
}

代码示例来源:origin: gocd/gocd

@Override
public final void checkout(File workDir, Revision revision) {
  try {
    if (workDir.exists()) {
      FileUtils.deleteQuietly(workDir);
    }
    setupWorkspace(workDir);
    LOGGER.debug("[TFS] Retrieving Files from Workspace {}, Working Folder {}, Revision {} ", workspace, workDir, revision);
    retrieveFiles(workDir, revision);
  } catch (Exception e) {
    String exceptionMessage = String.format("Failed while checking out into Working Folder: %s, Project Path: %s, Workspace: %s, Username: %s, Domain: %s, Root Cause: %s", workDir, projectPath,
        workspace,
        userName,
        domain, e.getMessage());
    throw new RuntimeException(exceptionMessage, e);
  } finally {
    clearMapping(workDir);
  }
}

代码示例来源:origin: gocd/gocd

NestedJarClassLoader(URL jarURL, ClassLoader parentClassLoader, String... excludes) {
  super(null);
  this.jarDir = new File(TEMP_DIR, UUID.randomUUID().toString());
  this.parentClassLoader = parentClassLoader;
  this.jarClassLoader = createLoaderForJar(jarURL);
  this.excludes = excludes;
  Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtils.deleteQuietly(jarDir)));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testDeleteQuietlyDir() throws IOException {
  final File testDirectory = new File(getTestDirectory(), "testDeleteQuietlyDir");
  final File testFile = new File(testDirectory, "testDeleteQuietlyFile");
  testDirectory.mkdirs();
  if (!testFile.getParentFile().exists()) {
    throw new IOException("Cannot create file " + testFile
        + " as the parent directory does not exist");
  }
  final BufferedOutputStream output =
      new BufferedOutputStream(new FileOutputStream(testFile));
  try {
    TestUtils.generateTestData(output, 0);
  } finally {
    IOUtils.closeQuietly(output);
  }
  assertTrue(testDirectory.exists());
  assertTrue(testFile.exists());
  FileUtils.deleteQuietly(testDirectory);
  assertFalse("Check No Exist", testDirectory.exists());
  assertFalse("Check No Exist", testFile.exists());
}

相关文章

FileUtils类方法