org.apache.flink.util.FileUtils.deletePathIfEmpty()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(111)

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

FileUtils.deletePathIfEmpty介绍

[英]Deletes the path if it is empty. A path can only be empty if it is a directory which does not contain any other directories/files.
[中]如果路径为空,则删除该路径。只有当路径是不包含任何其他目录/文件的目录时,路径才能为空。

代码示例

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

@Test
public void testDeletePathIfEmpty() throws IOException {
  final FileSystem localFs = FileSystem.getLocalFileSystem();
  final File dir = tmp.newFolder();
  assertTrue(dir.exists());
  final Path dirPath = new Path(dir.toURI());
  // deleting an empty directory should work
  assertTrue(FileUtils.deletePathIfEmpty(localFs, dirPath));
  // deleting a non existing directory should work
  assertTrue(FileUtils.deletePathIfEmpty(localFs, dirPath));
  // create a non-empty dir
  final File nonEmptyDir = tmp.newFolder();
  final Path nonEmptyDirPath = new Path(nonEmptyDir.toURI());
  new FileOutputStream(new File(nonEmptyDir, "filename")).close();
  assertFalse(FileUtils.deletePathIfEmpty(localFs, nonEmptyDirPath));
}

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

/**
 * Test that {@link FileUtils#deletePathIfEmpty(FileSystem, Path)} deletes the path if it is
 * empty. A path can only be empty if it is a directory which does not contain any
 * files/directories.
 */
@Test
public void testDeletePathIfEmpty() throws IOException {
  File file = temporaryFolder.newFile();
  File directory = temporaryFolder.newFolder();
  File directoryFile = new File(directory, UUID.randomUUID().toString());
  assertTrue(directoryFile.createNewFile());
  Path filePath = new Path(file.toURI());
  Path directoryPath = new Path(directory.toURI());
  Path directoryFilePath = new Path(directoryFile.toURI());
  FileSystem fs = FileSystem.getLocalFileSystem();
  // verify that the files have been created
  assertTrue(fs.exists(filePath));
  assertTrue(fs.exists(directoryFilePath));
  // delete the single file
  assertFalse(FileUtils.deletePathIfEmpty(fs, filePath));
  assertTrue(fs.exists(filePath));
  // try to delete the non-empty directory
  assertFalse(FileUtils.deletePathIfEmpty(fs, directoryPath));
  assertTrue(fs.exists(directoryPath));
  // delete the file contained in the directory
  assertTrue(fs.delete(directoryFilePath, false));
  // now the deletion should work
  assertTrue(FileUtils.deletePathIfEmpty(fs, directoryPath));
  assertFalse(fs.exists(directoryPath));
}

代码示例来源:origin: org.apache.flink/flink-runtime_2.10

FileUtils.deletePathIfEmpty(fs, basePath);
} catch (Exception ignored) {
  LOG.debug("Could not delete the parent directory {}.", basePath, ignored);

代码示例来源:origin: org.apache.flink/flink-runtime_2.10

/**
 * Discard the state by deleting the file that stores the state. If the parent directory
 * of the state is empty after deleting the state file, it is also deleted.
 * 
 * @throws Exception Thrown, if the file deletion (not the directory deletion) fails.
 */
@Override
public void discardState() throws Exception {
  getFileSystem().delete(filePath, false);
  try {
    FileUtils.deletePathIfEmpty(getFileSystem(), filePath.getParent());
  } catch (Exception ignored) {}
}

代码示例来源:origin: org.apache.flink/flink-runtime_2.10

/**
 * Discard the state by deleting the file that stores the state. If the parent directory
 * of the state is empty after deleting the state file, it is also deleted.
 *
 * @throws Exception Thrown, if the file deletion (not the directory deletion) fails.
 */
@Override
public void discardState() throws Exception {
  FileSystem fs = getFileSystem();
  fs.delete(filePath, false);
  if (fs.getKind() == FileSystemKind.FILE_SYSTEM) {
    try {
      FileUtils.deletePathIfEmpty(fs, filePath.getParent());
    } catch (Exception ignored) {}
  }
}

代码示例来源:origin: org.apache.flink/flink-runtime_2.10

FileUtils.deletePathIfEmpty(fs, basePath);
} catch (Exception parentDirDeletionFailure) {
  LOG.debug("Could not delete the parent directory {}.", basePath, parentDirDeletionFailure);

相关文章