Example usage for java.nio.file Files delete

List of usage examples for java.nio.file Files delete

Introduction

In this page you can find the example usage for java.nio.file Files delete.

Prototype

public static void delete(Path path) throws IOException 

Source Link

Document

Deletes a file.

Usage

From source file:org.forgerock.openidm.maintenance.upgrade.FileStateCheckerTest.java

@AfterMethod
public void deleteCheckSumsCopy() throws IOException {
    Files.delete(tempFile);
}

From source file:nl.salp.warcraft4j.config.PropertyWarcraft4jConfigTest.java

private static void delete(Path directory) throws Exception {
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override/*from w ww. j ava  2 s .c  o  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            if (e == null) {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            } else {
                throw e;
            }
        }
    });
}

From source file:org.savantbuild.io.tar.TarBuilder.java

/**
 * Builds the TAR file using the fileSets and Directories provided.
 *
 * @return The number of entries added to the TAR file including the directories.
 * @throws IOException If the build fails.
 *///from   w  w  w  . j a  v  a 2  s  . c  o m
public int build() throws IOException {
    if (Files.exists(file)) {
        Files.delete(file);
    }

    if (!Files.isDirectory(file.getParent())) {
        Files.createDirectories(file.getParent());
    }

    // Sort the file infos and add the directories
    Set<FileInfo> fileInfos = new TreeSet<>();
    for (FileSet fileSet : fileSets) {
        Set<Directory> dirs = fileSet.toDirectories();
        dirs.removeAll(directories);
        for (Directory dir : dirs) {
            directories.add(dir);
        }

        fileInfos.addAll(fileSet.toFileInfos());
    }

    int count = 0;
    OutputStream os = Files.newOutputStream(file);
    try (TarArchiveOutputStream tos = new TarArchiveOutputStream(compress ? new GZIPOutputStream(os) : os)) {
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        for (Directory directory : directories) {
            String name = directory.name;
            TarArchiveEntry entry = new TarArchiveEntry(name.endsWith("/") ? name : name + "/");
            if (directory.lastModifiedTime != null) {
                entry.setModTime(directory.lastModifiedTime.toMillis());
            }
            if (directory.mode != null) {
                entry.setMode(FileTools.toMode(directory.mode));
            }
            if (storeGroupName && directory.groupName != null) {
                entry.setGroupName(directory.groupName);
            }
            if (storeUserName && directory.userName != null) {
                entry.setUserName(directory.userName);
            }
            tos.putArchiveEntry(entry);
            tos.closeArchiveEntry();
            count++;
        }

        for (FileInfo fileInfo : fileInfos) {
            TarArchiveEntry entry = new TarArchiveEntry(fileInfo.relative.toString());
            entry.setModTime(fileInfo.lastModifiedTime.toMillis());
            if (storeGroupName) {
                entry.setGroupName(fileInfo.groupName);
            }
            if (storeUserName) {
                entry.setUserName(fileInfo.userName);
            }
            entry.setSize(fileInfo.size);
            entry.setMode(fileInfo.toMode());
            tos.putArchiveEntry(entry);
            Files.copy(fileInfo.origin, tos);
            tos.closeArchiveEntry();
            count++;
        }
    }

    return count;
}

From source file:energy.usef.environment.tool.util.FileUtil.java

public static void removeFile(String fileName, boolean failOnNotExists) {
    try {/*from w w w.j  ava2s . c  o  m*/
        Files.delete(Paths.get(fileName));
    } catch (IOException e) {
        if (failOnNotExists) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}

From source file:fi.johannes.kata.ocr.utils.files.ExistingFileConnection.java

public void writeNew(List<String> lines) throws IOException {
    Files.delete(p);
    Files.write(p, lines, StandardOpenOption.CREATE);
}

From source file:io.lavagna.service.importexport.FileUpload.java

@Override
void process(EventFull e, Event event, Date time, User user, ImportContext context, Path tempFile) {
    try {/*from  ww  w. j a  va2s.  c  o m*/
        Path p = Objects.requireNonNull(Read.readFile("files/" + e.getContent(), tempFile));
        CardDataUploadContentInfo fileData = Read.readObject("files/" + e.getContent() + ".json", tempFile,
                new TypeToken<CardDataUploadContentInfo>() {
                });
        ImmutablePair<Boolean, CardData> res = cardDataService.createFile(event.getValueString(),
                e.getContent(), fileData.getSize(), cardId(e), Files.newInputStream(p),
                fileData.getContentType(), user, time);
        if (res.getLeft()) {
            context.getFileId().put(event.getDataId(), res.getRight().getId());
        }
        Files.delete(p);
    } catch (IOException ioe) {
        throw new IllegalStateException("error while handling event FILE_UPLOAD for event: " + e, ioe);
    }
}

From source file:h2backup.BackupFileService.java

public Path backupDatabase(BackupMethod method, String directory, String filePrefix,
        String dateTimeFormatPattern) {
    try {//from w  ww. j av  a  2 s  .c om
        String fileNameWithoutExt = filePrefix + "_"
                + DateTimeFormatter.ofPattern(dateTimeFormatPattern).format(LocalDateTime.now(clock)) + "_"
                + method.getFileSuffix();
        final Path path = Paths.get(directory, fileNameWithoutExt + ZIP_EXTENSION);
        final Path tmpPath = Paths.get(directory, fileNameWithoutExt + TMP_SUFFIX + ZIP_EXTENSION);

        log.info("Starting backup to file {} using {} method", tmpPath, method);
        method.getStrategy().backupDatabase(dataSource, tmpPath.toString());

        if (Files.exists(path) && FileUtils.contentEquals(tmpPath.toFile(), path.toFile())) {
            log.info("Deleting backup {} because it is identical to previous backup {}", tmpPath, path);
            Files.delete(tmpPath);
            return null;
        }

        int maxIndex = 0;
        do {
            maxIndex++;
        } while (Files.exists(Paths.get(directory, fileNameWithoutExt + "_" + maxIndex + ZIP_EXTENSION)));

        for (int index = maxIndex; index >= 1; index--) {
            Path oldPath = Paths.get(directory,
                    fileNameWithoutExt + (index > 1 ? "_" + (index - 1) : "") + ZIP_EXTENSION);
            Path newPath = Paths.get(directory, fileNameWithoutExt + "_" + index + ZIP_EXTENSION);
            if (Files.exists(oldPath)) {
                log.debug("Moving file {} to {}", oldPath, newPath);
                Files.move(oldPath, newPath);
            }
        }

        Files.move(tmpPath, path);

        return path;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.neo4j.io.fs.FileUtils.java

public static void deletePathRecursively(Path path) throws IOException {
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override//w w w  .  jav a2  s .  com
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            deleteFileWithRetries(file, 0);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            if (e != null) {
                throw e;
            }
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:fi.hsl.parkandride.ExportQTypes.java

private static void deleteUnusedQTypes(Path packageDir, String... filesToRemove) throws IOException {
    for (String filename : filesToRemove) {
        Files.delete(packageDir.resolve(filename));
    }/*from  w w  w.ja v  a  2 s .  c  o  m*/
}

From source file:net.staticsnow.nexus.repository.apt.internal.hosted.CompressingTempFileStore.java

public void close() {
    for (FileHolder holder : holdersByKey.values()) {
        try {//from   w w w.j a  v  a  2s.  c o  m
            Files.delete(holder.bzTempFile);
        } catch (IOException e) {
        }
        try {
            Files.delete(holder.gzTempFile);
        } catch (IOException e) {
        }
    }
}