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

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

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

FileUtils.forceDelete介绍

[英]Deletes a file. 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.
  • You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)
    [中]删除一个文件。如果文件是一个目录,请删除它和所有子目录。
    文件之间的差异。delete()和此方法是:
    *要删除的目录不必为空。
    *无法删除文件或目录时会出现异常。(java.io.File方法返回布尔值)

代码示例

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

/**
   * Deletes the file object.
   * <p>
   * This implementation uses <code>FileUtils.forceDelete()</code>
   * if the file exists.
   *
   * @param fileToDelete  the file to delete, not null
   * @return Always returns {@code true}
   * @throws NullPointerException if the file is null
   * @throws IOException if an error occurs during file deletion
   */
  @Override
  protected boolean doDelete(final File fileToDelete) throws IOException {
    FileUtils.forceDelete(fileToDelete);
    return true;
  }
}

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

public static void rmr(String path) throws IOException {
  LOG.debug("Rmr path " + path);
  if (exists_file(path)) {
    FileUtils.forceDelete(new File(path));
  }
}

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

public void deleteVersion(long version) throws IOException {
  File versionFile = new File(versionPath(version));
  File tokenFile = new File(tokenPath(version));
  if (tokenFile.exists()) {
    FileUtils.forceDelete(tokenFile);
  }
  if (versionFile.exists()) {
    FileUtils.forceDelete(versionFile);
  }
}

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

public static void rmr(String path) throws IOException {
  LOG.debug("Rmr path " + path);
  if (exists_file(path)) {
    FileUtils.forceDelete(new File(path));
  }
}

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

/**
 * Cleans a directory without deleting it.
 *
 * @param directory directory to clean
 * @throws IOException              in case cleaning is unsuccessful
 * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory
 */
public static void cleanDirectory(final File directory) throws IOException {
  final File[] files = verifiedListFiles(directory);
  IOException exception = null;
  for (final File file : files) {
    try {
      forceDelete(file);
    } catch (final IOException ioe) {
      exception = ioe;
    }
  }
  if (null != exception) {
    throw exception;
  }
}

代码示例来源:origin: apache/hive

private void cleanupSessionLogDir() {
 // In case of test, if we might not want to remove the log directory
 if (isOperationLogEnabled && sessionConf.getBoolVar(ConfVars.HIVE_TESTING_REMOVE_LOGS)) {
  try {
   FileUtils.forceDelete(sessionLogDir);
   LOG.info("Operation log session directory is deleted: "
     + sessionLogDir.getAbsolutePath());
  } catch (Exception e) {
   LOG.error("Failed to cleanup session log dir: " + sessionHandle, e);
  }
 }
}

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

private void destroyBuffer(PinotDataBuffer buffer, File mmapFile)
   throws IOException {
  if (buffer != null) {
   buffer.close();
   if (mmapFile.exists()) {
    FileUtils.forceDelete(mmapFile);
   }
  }
 }
}

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

public static File newFile(final File testDirectory, final String filename) throws IOException {
  final File destination = new File(testDirectory, filename);
  /*
  assertTrue( filename + "Test output data file shouldn't previously exist",
        !destination.exists() );
  */
  if (destination.exists()) {
    FileUtils.forceDelete(destination);
  }
  return destination;
}

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

@AfterClass
public static void teardown() throws IOException
{
 FileUtils.forceDelete(TEST_DIR);
 for (File dir : FIREHOSE_TMP_DIRS) {
  FileUtils.forceDelete(dir);
 }
}

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

private File createFirehoseTmpDir(String dirSuffix) throws IOException
{
 final File firehoseTempDir = File.createTempFile(
   SqlFirehoseFactoryTest.class.getSimpleName(),
   dirSuffix
 );
 FileUtils.forceDelete(firehoseTempDir);
 FileUtils.forceMkdir(firehoseTempDir);
 FIREHOSE_TMP_DIRS.add(firehoseTempDir);
 return firehoseTempDir;
}

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

@BeforeClass
public static void setup() throws IOException
{
 TEST_DIR = File.createTempFile(SqlFirehoseFactoryTest.class.getSimpleName(), "testDir");
 FileUtils.forceDelete(TEST_DIR);
 FileUtils.forceMkdir(TEST_DIR);
}

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

@After
public void teardown() throws IOException
{
 FileUtils.forceDelete(TEST_DIR);
}

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

@After
public void teardown() throws IOException
{
 inputStream.close();
 FileUtils.forceDelete(testFile);
}

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

@Test
public void testForceDeleteAFile1() throws Exception {
  final File destination = new File(getTestDirectory(), "copy1.txt");
  destination.createNewFile();
  assertTrue("Copy1.txt doesn't exist to delete", destination.exists());
  FileUtils.forceDelete(destination);
  assertTrue("Check No Exist", !destination.exists());
}

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

@Test
public void testForceDeleteAFile2() throws Exception {
  final File destination = new File(getTestDirectory(), "copy2.txt");
  destination.createNewFile();
  assertTrue("Copy2.txt doesn't exist to delete", destination.exists());
  FileUtils.forceDelete(destination);
  assertTrue("Check No Exist", !destination.exists());
}

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

@AfterClass
public static void tearDownAfterClass() throws KettleException, IOException {
 if ( repo != null ) {
  repo.disconnect();
 }
 FileUtils.forceDelete( new File( baseDirName.toString() ) );
}

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

@Test
public void testForceDeleteAFile3() throws Exception {
  final File destination = new File(getTestDirectory(), "no_such_file");
  assertTrue("Check No Exist", !destination.exists());
  try {
    FileUtils.forceDelete(destination);
    fail("Should generate FileNotFoundException");
  } catch (final FileNotFoundException ignored) {
  }
}

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

@Test
public void testForceDeleteDir() throws Exception {
  final File testDirectory = getTestDirectory();
  assertTrue("TestDirectory must exist", testDirectory.exists());
  FileUtils.forceDelete(testDirectory);
  assertFalse("TestDirectory must not exist", testDirectory.exists());
}

代码示例来源:origin: SonarSource/sonarqube

@Test
 public void create_temp_dir_if_missing() throws Exception {
  ServerFileSystem fs = mock(ServerFileSystem.class);
  File tmpDir = temp.newFolder();
  when(fs.getTempDir()).thenReturn(tmpDir);
  FileUtils.forceDelete(tmpDir);

  TempFolder folder = underTest.provide(fs);
  assertThat(folder).isNotNull();
  File newDir = folder.newDir();
  assertThat(newDir).exists().isDirectory();
  assertThat(newDir.getParentFile().getCanonicalPath()).startsWith(tmpDir.getCanonicalPath());
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void read_file_fails_if_file_does_not_exist() throws Exception {
 thrown.expect(ContextException.class);
 thrown.expectMessage("Unable to read message");
 File file = temp.newFile();
 FileUtils.forceDelete(file);
 Protobuf.read(file, Fake.parser());
}

相关文章

FileUtils类方法