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

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

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

FileUtils.cleanDirectory介绍

[英]Cleans a directory without deleting it.
[中]清除目录而不删除它。

代码示例

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

public void cleanDir() throws IOException {
  File destDirFile = new File(destDir);
  FileUtils.forceMkdir(destDirFile);
  FileUtils.cleanDirectory(destDirFile);
}

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

public static void cleanRocksDbLocalDir(String rocksDbDir) throws IOException {
    File file = new File(rocksDbDir);
    if (file.exists()) {
      FileUtils.cleanDirectory(file);
    } else {
      FileUtils.forceMkdir(file);
    }
  }
}

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

cleanDirectory(file);

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

/**
 * Deletes a directory recursively.
 *
 * @param directory directory to delete
 * @throws IOException              in case deletion is unsuccessful
 * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory
 */
public static void deleteDirectory(final File directory) throws IOException {
  if (!directory.exists()) {
    return;
  }
  if (!isSymlink(directory)) {
    cleanDirectory(directory);
  }
  if (!directory.delete()) {
    final String message =
        "Unable to delete directory " + directory + ".";
    throw new IOException(message);
  }
}

代码示例来源:origin: Netflix/conductor

default void cleanDataDir(String path) {
  File dataDir = new File(path);
  try {
    logger.info("Deleting contents of data dir {}", path);
    if (dataDir.exists()) {
      FileUtils.cleanDirectory(dataDir);
    }
  } catch (IOException e) {
    logger.error(String.format("Failed to delete ES data dir: %s", dataDir.getAbsolutePath()), e);
  }
}

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

private void createWorkingDirectoryIfNotExist(File buildWorkingDirectory) {
    if (assignment.shouldCleanWorkingDir() && buildWorkingDirectory.exists()) {
      try {
        FileUtils.cleanDirectory(buildWorkingDirectory);
        goPublisher.consumeLineWithPrefix("Cleaning working directory \"" + buildWorkingDirectory.getAbsolutePath() + "\" since stage is configured to clean working directory");
      } catch (IOException e) {
        bomb("Clean working directory is set to true. Unable to clean working directory for agent: " + buildWorkingDirectory.getAbsolutePath() + ", with error: " + e.getMessage());
      }
    }
    if (!buildWorkingDirectory.exists()) {
      if (!buildWorkingDirectory.mkdirs()) {
        bomb("Unable to create working directory for agent: " + buildWorkingDirectory.getAbsolutePath());
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration-samples

@After
public void tearDown() throws IOException {
  File inDir = new File("/tmp/in");
  if (inDir.exists()) {
    FileUtils.cleanDirectory(inDir);
  }
  File outDir = new File("/tmp/out");
  if (outDir.exists()) {
    FileUtils.cleanDirectory(outDir);
  }
}

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

@Test
public void testCleanEmpty() throws Exception {
  assertEquals(0, top.list().length);
  FileUtils.cleanDirectory(top);
  assertEquals(0, top.list().length);
}

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

private void emptyTestDirectory() throws IOException {
    //Delete Virtual File System folder
    Path dir = Paths.get(sshTestServer.getVirtualFileSystemPath());
    FileUtils.cleanDirectory(dir.toFile());
  }
}

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

@Override
public void initialize() {
  cleanupOldPluginDirectories();
  try {
    File bundledPluginsDirectory = new File(systemEnvironment.get(PLUGIN_GO_PROVIDED_PATH));
    if (shouldReplaceBundledPlugins(bundledPluginsDirectory)) {
      FileUtils.cleanDirectory(bundledPluginsDirectory);
      zipUtil.unzip(getPluginsZipStream(), bundledPluginsDirectory);
    }
    pluginManager.startInfrastructure(true);
  } catch (Exception e) {
    LOG.error("Could not extract bundled plugins to default bundled directory", e);
  }
}

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

@Test
public void testDeletesNested() throws Exception {
  final File nested = new File(top, "nested");
  assertTrue(nested.mkdirs());
  FileUtils.touch(new File(nested, "file"));
  assertEquals(1, top.list().length);
  FileUtils.cleanDirectory(top);
  assertEquals(0, top.list().length);
}

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

@Test
public void testThrowsOnNullList() throws Exception {
  if (System.getProperty("os.name").startsWith("Win")  ||  !chmod(top, 0, false)) {
    // test wont work if we can't restrict permissions on the
    // directory, so skip it.
    return;
  }
  try {
    FileUtils.cleanDirectory(top);
    fail("expected IOException");
  } catch (final IOException e) {
    assertEquals("Failed to list contents of " +
        top.getAbsolutePath(), e.getMessage());
  }
}

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

@Before
public void setup() throws Exception {
  this.createTestMetadata();
  FileUtils.forceMkdir(new File(dstPath));
  FileUtils.cleanDirectory(new File(dstPath));
}

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

@Test
public void testDeletesRegular() throws Exception {
  FileUtils.touch(new File(top, "regular"));
  FileUtils.touch(new File(top, ".hidden"));
  assertEquals(2, top.list().length);
  FileUtils.cleanDirectory(top);
  assertEquals(0, top.list().length);
}

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

@Test
public void testStillClearsIfGivenDirectoryIsASymlink() throws Exception {
  if (System.getProperty("os.name").startsWith("Win")) {
    // cant create symlinks in windows.
    return;
  }
  final File randomDirectory = new File(top, "randomDir");
  assertTrue(randomDirectory.mkdirs());
  FileUtils.touch(new File(randomDirectory, "randomfile"));
  assertEquals(1, randomDirectory.list().length);
  final File symlinkDirectory = new File(top, "fakeDir");
  setupSymlink(randomDirectory, symlinkDirectory);
  FileUtils.cleanDirectory(symlinkDirectory);
  assertEquals(0, symlinkDirectory.list().length);
  assertEquals(0, randomDirectory.list().length);
}

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

@Test
public void testThrowsOnCannotDeleteFile() throws Exception {
  final File file = new File(top, "restricted");
  FileUtils.touch(file);
  if (System.getProperty("os.name").startsWith("Win")  ||  !chmod(top, 500, false)) {
    // test wont work if we can't restrict permissions on the
    // directory, so skip it.
    return;
  }
  try {
    FileUtils.cleanDirectory(top);
    fail("expected IOException");
  } catch (final IOException e) {
    assertEquals("Unable to delete file: " +
        file.getAbsolutePath(), e.getMessage());
  }
}

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

@Test
public void testCleanDirWithASymlinkDir() throws Exception {
  if (System.getProperty("os.name").startsWith("Win")) {
    // cant create symlinks in windows.
    return;
  }
  final File realOuter = new File(top, "realouter");
  assertTrue(realOuter.mkdirs());
  final File realInner = new File(realOuter, "realinner");
  assertTrue(realInner.mkdirs());
  FileUtils.touch(new File(realInner, "file1"));
  assertEquals(1, realInner.list().length);
  final File randomDirectory = new File(top, "randomDir");
  assertTrue(randomDirectory.mkdirs());
  FileUtils.touch(new File(randomDirectory, "randomfile"));
  assertEquals(1, randomDirectory.list().length);
  final File symlinkDirectory = new File(realOuter, "fakeinner");
  setupSymlink(randomDirectory, symlinkDirectory);
  assertEquals(1, symlinkDirectory.list().length);
  // assert contents of the real directory were removed including the symlink
  FileUtils.cleanDirectory(realOuter);
  assertEquals(0, realOuter.list().length);
  // ensure that the contents of the symlink were NOT removed.
  assertEquals("Contents of sym link should not have been removed", 1, randomDirectory.list().length);
}

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

@Test
public void testCleanDirWithSymlinkFile() throws Exception {
  if (System.getProperty("os.name").startsWith("Win")) {
    // cant create symlinks in windows.
    return;
  }
  final File realOuter = new File(top, "realouter");
  assertTrue(realOuter.mkdirs());
  final File realInner = new File(realOuter, "realinner");
  assertTrue(realInner.mkdirs());
  final File realFile = new File(realInner, "file1");
  FileUtils.touch(realFile);
  assertEquals(1, realInner.list().length);
  final File randomFile = new File(top, "randomfile");
  FileUtils.touch(randomFile);
  final File symlinkFile = new File(realInner, "fakeinner");
  setupSymlink(randomFile, symlinkFile);
  assertEquals(2, realInner.list().length);
  // assert contents of the real directory were removed including the symlink
  FileUtils.cleanDirectory(realOuter);
  assertEquals(0, realOuter.list().length);
  // ensure that the contents of the symlink were NOT removed.
  assertTrue(randomFile.exists());
  assertFalse(symlinkFile.exists());
}

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

@Test
public void testCleanDirWithParentSymlinks() throws Exception {
  if (System.getProperty("os.name").startsWith("Win")) {
    // cant create symlinks in windows.
    return;
  }
  final File realParent = new File(top, "realparent");
  assertTrue(realParent.mkdirs());
  final File realInner = new File(realParent, "realinner");
  assertTrue(realInner.mkdirs());
  FileUtils.touch(new File(realInner, "file1"));
  assertEquals(1, realInner.list().length);
  final File randomDirectory = new File(top, "randomDir");
  assertTrue(randomDirectory.mkdirs());
  FileUtils.touch(new File(randomDirectory, "randomfile"));
  assertEquals(1, randomDirectory.list().length);
  final File symlinkDirectory = new File(realParent, "fakeinner");
  setupSymlink(randomDirectory, symlinkDirectory);
  assertEquals(1, symlinkDirectory.list().length);
  final File symlinkParentDirectory = new File(top, "fakeouter");
  setupSymlink(realParent, symlinkParentDirectory);
  // assert contents of the real directory were removed including the symlink
  FileUtils.cleanDirectory(symlinkParentDirectory);// should clean the contents of this but not recurse into other links
  assertEquals(0, symlinkParentDirectory.list().length);
  assertEquals(0, realParent.list().length);
  // ensure that the contents of the symlink were NOT removed.
  assertEquals("Contents of sym link should not have been removed", 1, randomDirectory.list().length);
}

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

@Override
  public boolean execute(BuildCommand command, BuildSession buildSession) {
    File dir = buildSession.resolveRelativeDir(command.getWorkingDirectory(), command.getStringArg("path"));
    String[] allowed = command.getArrayArg("allowed");
    if (allowed.length == 0) {
      try {
        FileUtils.cleanDirectory(dir);
      } catch (IOException e) {
        return false;
      }
    } else {
      DirectoryCleaner cleaner = new DirectoryCleaner(dir, buildSession.processOutputStreamConsumer());
      cleaner.allowed(allowed);
      cleaner.clean();
    }
    return true;
  }
}

相关文章

FileUtils类方法