java.nio.file.Files.deleteIfExists()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(323)

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

Files.deleteIfExists介绍

暂无

代码示例

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

@Override
 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  Files.deleteIfExists(dir);
  return FileVisitResult.CONTINUE;
 }
}

代码示例来源:origin: google/guava

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
  throws IOException {
 java.nio.file.Files.deleteIfExists(file);
 return FileVisitResult.CONTINUE;
}

代码示例来源:origin: google/guava

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
  throws IOException {
 Files.deleteIfExists(file);
 return FileVisitResult.CONTINUE;
}

代码示例来源:origin: google/guava

@Override
 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  if (exc != null) {
   return FileVisitResult.TERMINATE;
  }
  java.nio.file.Files.deleteIfExists(dir);
  return FileVisitResult.CONTINUE;
 }
});

代码示例来源:origin: google/guava

@Override
 public FileVisitResult postVisitDirectory(Path dir, IOException exc)
   throws IOException {
  if (exc != null) {
   return FileVisitResult.TERMINATE;
  }
  Files.deleteIfExists(dir);
  return FileVisitResult.CONTINUE;
 }
});

代码示例来源:origin: Alluxio/alluxio

/**
 * Deletes the file or directory.
 *
 * @param path pathname string of file or directory
 */
public static void delete(String path) throws IOException {
 if (!Files.deleteIfExists(Paths.get(path))) {
  throw new IOException("Failed to delete " + path);
 }
}

代码示例来源:origin: jenkinsci/jenkins

private void closeAndDeleteTempFile() throws IOException {
  // one way or the other, temporary file should be deleted.
  try {
    close();
  } finally {
    Files.deleteIfExists(tmpPath);
  }
}

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

/**
 * Delete this message set from the filesystem
 * @throws IOException if deletion fails due to an I/O error
 * @return  {@code true} if the file was deleted by this method; {@code false} if the file could not be deleted
 *          because it did not exist
 */
public boolean deleteIfExists() throws IOException {
  Utils.closeQuietly(channel, "FileChannel");
  return Files.deleteIfExists(file.toPath());
}

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

private boolean tryClose() {
  try {
    Files.deleteIfExists(file.toPath());
    return true;
  } catch (Throwable t) {
    ExceptionUtils.rethrowIfFatalError(t);
  }
  return false;
}

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

private static void deleteFileOrDirectoryInternal(File file) throws IOException {
  if (file.isDirectory()) {
    // file exists and is directory
    deleteDirectoryInternal(file);
  }
  else {
    // if the file is already gone (concurrently), we don't mind
    Files.deleteIfExists(file.toPath());
  }
  // else: already deleted
}

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

static void completelyRemoveUnusedUser(Path localBaseDir, String user) throws IOException {
  Path userFileCacheDir = getLocalUserFileCacheDir(localBaseDir, user);
  // baseDir/supervisor/usercache/user1/filecache/files
  Files.deleteIfExists(getCacheDirForFiles(userFileCacheDir));
  // baseDir/supervisor/usercache/user1/filecache/archives
  Files.deleteIfExists(getCacheDirForArchives(userFileCacheDir));
  // baseDir/supervisor/usercache/user1/filecache
  Files.deleteIfExists(userFileCacheDir);
  // baseDir/supervisor/usercache/user1
  Files.deleteIfExists(getLocalUserDir(localBaseDir, user));
}

代码示例来源:origin: siacs/Conversations

@Override
public final byte[] remove(Object key) {
  byte[] data = get(key);
  try {
    Files.deleteIfExists(cacheDirectory.resolve(key.toString()));
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
  return data;
}

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

private static void createHtmlFile(DocumentingRestEndpoint restEndpoint, RestAPIVersion apiVersion, Path outputFile) throws IOException {
  StringBuilder html = new StringBuilder();
  List<MessageHeaders> specs = restEndpoint.getSpecs().stream()
    .filter(spec -> spec.getSupportedAPIVersions().contains(apiVersion))
    .collect(Collectors.toList());
  specs.forEach(spec -> html.append(createHtmlEntry(spec)));
  Files.deleteIfExists(outputFile);
  Files.write(outputFile, html.toString().getBytes(StandardCharsets.UTF_8));
}

代码示例来源:origin: jenkinsci/jenkins

private void removeOrMakeRemovableThenRemove(@Nonnull Path path) throws IOException {
  pathChecker.check(path);
  try {
    Files.deleteIfExists(path);
  } catch (IOException e) {
    makeRemovable(path);
    try {
      Files.deleteIfExists(path);
    } catch (IOException e2) {
      // see https://java.net/projects/hudson/lists/users/archive/2008-05/message/357
      // I suspect other processes putting files in this directory
      if (Files.isDirectory(path)) {
        List<String> entries;
        try (Stream<Path> children = Files.list(path)) {
          entries = children.map(Path::toString).collect(Collectors.toList());
        }
        throw new CompositeIOException("Unable to remove directory " + path + " with directory contents: " + entries, e, e2);
      }
      throw new CompositeIOException("Unable to remove file " + path, e, e2);
    }
  }
}

代码示例来源:origin: pxb1988/dex2jar

public static FileSystem createZip(Path output) throws IOException {
  Map<String, Object> env = new HashMap<>();
  env.put("create", "true");
  Files.deleteIfExists(output);
  createParentDirectories(output);
  for (FileSystemProvider p : FileSystemProvider.installedProviders()) {
    String s = p.getScheme();
    if ("jar".equals(s) || "zip".equalsIgnoreCase(s)) {
      return p.newFileSystem(output, env);
    }
  }
  throw new IOException("cant find zipfs support");
}

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

static void createGzippedFile(String filePath) throws IOException {
  Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class);
  Resource resource = new FileSystemResource(location.createRelative(filePath).getFile());
  Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz");
  Files.deleteIfExists(gzFilePath);
  File gzFile = Files.createFile(gzFilePath).toFile();
  GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
  FileCopyUtils.copy(resource.getInputStream(), out);
  gzFile.deleteOnExit();
}

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

static void createGzippedFile(String filePath) throws IOException {
  Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class);
  Resource resource = new FileSystemResource(location.createRelative(filePath).getFile());
  Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz");
  Files.deleteIfExists(gzFilePath);
  File gzFile = Files.createFile(gzFilePath).toFile();
  GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
  FileCopyUtils.copy(resource.getInputStream(), out);
  gzFile.deleteOnExit();
}

代码示例来源:origin: google/guava

@Override
 public void run() {
  boolean createSymlink = false;
  while (!Thread.interrupted()) {
   try {
    // trying to switch between a real directory and a symlink (dir -> /a)
    if (Files.deleteIfExists(file)) {
     if (createSymlink) {
      Files.createSymbolicLink(file, target);
     } else {
      Files.createDirectory(file);
     }
     createSymlink = !createSymlink;
    }
   } catch (IOException tolerated) {
    // it's expected that some of these will fail
   }
   Thread.yield();
  }
 }
});

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

@After
  public void cleanUp() throws IOException {
    Files.deleteIfExists(Paths.get(path));
  }
}

代码示例来源:origin: prestodb/presto

@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = "Configured staging path is not a directory: .*")
public void testCreateWithStagingDirectoryFile()
    throws Exception
{
  java.nio.file.Path staging = createTempFile("staging", null);
  // staging = /tmp/stagingXXX.tmp
  try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
    MockAmazonS3 s3 = new MockAmazonS3();
    Configuration conf = new Configuration();
    conf.set(S3_STAGING_DIRECTORY, staging.toString());
    fs.initialize(new URI("s3n://test-bucket/"), conf);
    fs.setS3Client(s3);
    fs.create(new Path("s3n://test-bucket/test"));
  }
  finally {
    Files.deleteIfExists(staging);
  }
}

相关文章

微信公众号

最新文章

更多