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.apache.hadoop.hive.ql.log.TestSlidingFilenameRolloverStrategy.java

private void deleteLogFiles(Path parent, String fileName) throws IOException {
    DirectoryStream<Path> stream = Files.newDirectoryStream(parent, fileName + ".*");
    for (Path path : stream) {
        Files.delete(path);
    }/*from   w w w .  j  a va 2s  .c o  m*/
}

From source file:com.ignorelist.kassandra.steam.scraper.FileCache.java

private void putNonBlocking(String key, InputStream value) throws IOException {
    final Path cacheFile = buildCacheFile(key);
    OutputStream outputStream = new GZIPOutputStream(Files.newOutputStream(cacheFile));
    try {//from   w  w  w  .  j av a 2s.co  m
        IOUtils.copy(value, outputStream);
    } catch (IOException e) {
        IOUtils.closeQuietly(outputStream);
        Files.delete(cacheFile);
        throw e;
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:org.ballerinalang.composer.service.fs.LocalFileSystem.java

@Override
public void delete(String path) throws IOException {
    Path ioPath = Paths.get(path);
    if (ioPath.toFile().isDirectory()) {
        Files.walk(ioPath, FileVisitOption.FOLLOW_LINKS).sorted(Comparator.reverseOrder()).map(Path::toFile)
                .forEach(File::delete);
    } else {/*ww  w  .  j av  a 2s  .co m*/
        Files.delete(ioPath);
    }
}

From source file:FileGameAccess.java

@Override
public void clearPersistance() {
    File folder = new File(System.getProperty("user.dir"));
    File[] filesInFolder = folder.listFiles();
    List<User> users = new ArrayList<>();
    for (File f : filesInFolder)
        if (FilenameUtils.getExtension(f.getName()).equals("commands")
                || FilenameUtils.getExtension(f.getName()).equals("catanmodel"))
            try {
                Files.delete(f.toPath());
            } catch (IOException ex) {
                Logger.getLogger(FileGameAccess.class.getName()).log(Level.SEVERE, null, ex);
            }//from  ww  w.  j a v a  2  s. com

}

From source file:org.apache.solr.ltr.TestRerankBase.java

protected static SortedMap<ServletHolder, String> setupTestInit(String solrconfig, String schema,
        boolean isPersistent) throws Exception {
    tmpSolrHome = createTempDir().toFile();
    tmpConfDir = new File(tmpSolrHome, CONF_DIR);
    tmpConfDir.deleteOnExit();// www. j ava  2 s  .  c o m
    FileUtils.copyDirectory(new File(TEST_HOME()), tmpSolrHome.getAbsoluteFile());

    final File fstore = new File(tmpConfDir, FEATURE_FILE_NAME);
    final File mstore = new File(tmpConfDir, MODEL_FILE_NAME);

    if (isPersistent) {
        fstorefile = fstore;
        mstorefile = mstore;
    }

    if (fstore.exists()) {
        log.info("remove feature store config file in {}", fstore.getAbsolutePath());
        Files.delete(fstore.toPath());
    }
    if (mstore.exists()) {
        log.info("remove model store config file in {}", mstore.getAbsolutePath());
        Files.delete(mstore.toPath());
    }
    if (!solrconfig.equals("solrconfig.xml")) {
        FileUtils.copyFile(new File(tmpSolrHome.getAbsolutePath() + "/collection1/conf/" + solrconfig),
                new File(tmpSolrHome.getAbsolutePath() + "/collection1/conf/solrconfig.xml"));
    }
    if (!schema.equals("schema.xml")) {
        FileUtils.copyFile(new File(tmpSolrHome.getAbsolutePath() + "/collection1/conf/" + schema),
                new File(tmpSolrHome.getAbsolutePath() + "/collection1/conf/schema.xml"));
    }

    final SortedMap<ServletHolder, String> extraServlets = new TreeMap<>();
    final ServletHolder solrRestApi = new ServletHolder("SolrSchemaRestApi", ServerServlet.class);
    solrRestApi.setInitParameter("org.restlet.application", SolrSchemaRestApi.class.getCanonicalName());
    solrRestApi.setInitParameter("storageIO",
            ManagedResourceStorage.InMemoryStorageIO.class.getCanonicalName());
    extraServlets.put(solrRestApi, PARENT_ENDPOINT);

    System.setProperty("managed.schema.mutable", "true");

    return extraServlets;
}

From source file:com.clust4j.algo.NearestCentroidTests.java

@Test
@Override//from w  w w  .  j av  a 2  s  . c om
public void testSerialization() throws IOException, ClassNotFoundException {
    NearestCentroid nn = new NearestCentroid(data_, target_, new NearestCentroidParameters().setVerbose(true))
            .fit();

    final int[] c = nn.getLabels();
    nn.saveObject(new FileOutputStream(TestSuite.tmpSerPath));
    assertTrue(TestSuite.file.exists());

    NearestCentroid nn2 = (NearestCentroid) NearestCentroid
            .loadObject(new FileInputStream(TestSuite.tmpSerPath));
    assertTrue(VecUtils.equalsExactly(c, nn2.getLabels()));
    assertTrue(nn2.equals(nn));
    Files.delete(TestSuite.path);
}

From source file:notaql.performance.PerformanceTest.java

private static void deleteRecursive(Path path) throws IOException {
    if (!Files.exists(path))
        return;/*from w  w w. ja  v a2  s .c o m*/

    Files.walk(path).sorted((a, b) -> b.compareTo(a)). // reverse; files before dirs
            forEach(p -> {
                try {
                    Files.delete(p);
                } catch (IOException e) {
                    /* ... */ }
            });
}

From source file:io.undertow.servlet.test.defaultservlet.DefaultServletCachingTestCase.java

@Test
public void testFileExistanceCheckCached() throws IOException, InterruptedException {
    TestHttpClient client = new TestHttpClient();
    String fileName = new SecureRandomSessionIdGenerator().createSessionId() + ".html";
    try {//from  ww w  .j  a  va2 s.  c  o m
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/" + fileName);
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);

        Path f = tmpDir.resolve(fileName);
        Files.write(f, "hello".getBytes());
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/" + fileName);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        Thread.sleep(METADATA_MAX_AGE);

        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/" + fileName);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("hello", response);
        Files.delete(f);
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:net.sf.jabref.logic.exporter.FileSaveSession.java

@Override
public void commit(Path file) throws SaveException {
    if (file == null) {
        return;//from  w  w  w  . j  av a  2s  .  c o  m
    }
    if (backup && Files.exists(file)) {
        Path fileName = file.getFileName();
        Path backupFile = file.resolveSibling(fileName + BACKUP_EXTENSION);
        try {
            FileUtil.copyFile(file.toFile(), backupFile.toFile(), true);
        } catch (IOException ex) {
            LOGGER.error("Problem copying file", ex);
            throw SaveException.BACKUP_CREATION;
        }
    }
    try {
        if (useLockFile) {
            try {
                if (FileBasedLock.createLockFile(file)) {
                    // Oops, the lock file already existed. Try to wait it out:
                    if (!FileBasedLock.waitForFileLock(file, 10)) {
                        throw SaveException.FILE_LOCKED;
                    }
                }
            } catch (IOException ex) {
                LOGGER.error("Error when creating lock file.", ex);
            }
        }

        FileUtil.copyFile(temporaryFile.toFile(), file.toFile(), true);
    } catch (IOException ex2) {
        // If something happens here, what can we do to correct the problem? The file is corrupted, but we still
        // have a clean copy in tmp. However, we just failed to copy tmp to file, so it's not likely that
        // repeating the action will have a different result.
        // On the other hand, our temporary file should still be clean, and won't be deleted.
        throw new SaveException("Save failed while committing changes: " + ex2.getMessage(),
                Localization.lang("Save failed while committing changes: %0", ex2.getMessage()));
    } finally {
        if (useLockFile) {
            FileBasedLock.deleteLockFile(file);
        }
    }
    try {
        Files.delete(temporaryFile);
    } catch (IOException e) {
        LOGGER.warn("Cannot delete temporary file", e);
    }
}

From source file:org.carcv.impl.core.io.FFMPEGVideoHandlerIT.java

/**
 * Test method for/*w  w  w. j ava 2 s .  c  o  m*/
 * {@link org.carcv.impl.core.io.FFMPEGVideoHandler#splitIntoFrames(java.nio.file.Path, int, java.nio.file.Path)}.
 *
 * @throws IOException
 */
@Test
public void testSplitIntoFramesPathIntPath() throws IOException {
    FFMPEGVideoHandler fvh = new FFMPEGVideoHandler();
    FFMPEGVideoHandler.copyCarImagesToDir(entry.getCarImages(), videoDir);
    Path video = fvh.generateVideo(videoDir, FFMPEGVideoHandler.defaultFrameRate);

    Path dir = Paths.get(video.toString() + ".custom_dir");
    assertTrue("Split failed.", fvh.splitIntoFrames(video, FFMPEGVideoHandler.defaultFrameRate, dir));

    DirectoryStream<Path> paths = Files.newDirectoryStream(dir);
    int counter = 0;
    for (@SuppressWarnings("unused")
    Path p : paths) {
        counter++;
    }
    assertEquals(entry.getCarImages().size(), counter);

    Files.delete(video);
    DirectoryWatcher.deleteDirectory(dir);
}