@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { java.nio.file.Files.deleteIfExists(file); return FileVisitResult.CONTINUE; }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.deleteIfExists(file); return FileVisitResult.CONTINUE; }
@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; } });
@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc != null) { return FileVisitResult.TERMINATE; } Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } });
/** * 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); } }
private void closeAndDeleteTempFile() throws IOException { // one way or the other, temporary file should be deleted. try { close(); } finally { Files.deleteIfExists(tmpPath); } }
/** * 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()); }
private boolean tryClose() { try { Files.deleteIfExists(file.toPath()); return true; } catch (Throwable t) { ExceptionUtils.rethrowIfFatalError(t); } return false; }
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 }
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)); }
@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; }
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)); }
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); } } }
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"); }
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(); }
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(); }
@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(); } } });
@After public void cleanUp() throws IOException { Files.deleteIfExists(Paths.get(path)); } }
@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); } }