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:com.clust4j.algo.pipeline.PipelineTest.java

@Test
public void testA() throws FileNotFoundException, IOException, ClassNotFoundException {
    final double[][] data = new double[][] { new double[] { 0.005, 0.182751, 0.1284 },
            new double[] { 3.65816, 0.29518, 2.123316 }, new double[] { 4.1234, 0.27395, 1.8900002 } };

    final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(data);
    final KMeansParameters planner = new KMeansParameters(2).setVerbose(true);

    // Build the pipeline
    final UnsupervisedPipeline<KMeans> pipe = new UnsupervisedPipeline<KMeans>(planner,
            new PreProcessor[] { new StandardScaler(),
                    new MeanImputation(new MeanImputation.MeanImputationPlanner().setVerbose(true)) // Will create a warning
            });/*w  ww  .j  a v  a  2  s.c  o m*/

    final KMeans km = pipe.fit(mat);

    assertTrue(km.getLabels()[0] == 0 && km.getLabels()[1] == 1);
    assertTrue(km.getLabels()[1] == km.getLabels()[2]);
    assertTrue(km.didConverge());
    System.out.println();

    pipe.saveObject(new FileOutputStream(TestSuite.tmpSerPath));
    assertTrue(TestSuite.file.exists());

    @SuppressWarnings("unchecked")
    UnsupervisedPipeline<KMeans> pipe2 = (UnsupervisedPipeline<KMeans>) UnsupervisedPipeline
            .loadObject(new FileInputStream(TestSuite.tmpSerPath));

    final KMeans km2 = pipe2.fit(mat);

    assertTrue(km2.getLabels()[0] == 0 && km2.getLabels()[1] == 1);
    assertTrue(km2.getLabels()[1] == km2.getLabels()[2]);
    assertTrue(km2.didConverge());

    Files.delete(TestSuite.path);
}

From source file:com.rptools.io.TableFileParser.java

/**
 * Save new json file if it doesn't exist. Delete the text file.
 *
 * @param file Path to file/*w  w  w .jav a 2  s .c  o m*/
 * @param builder RPTable builder to print to JSON file
 * @return Boolean: If the .json file already existed and will be parsed separately
 */
private boolean updateResourceFiles(Path file, RPTable.Builder builder) throws IOException {
    File json = new File(file.toString().replace(EXT_TXT, EXT_JSON));
    Files.delete(file);
    boolean isNew = json.createNewFile();
    Files.write(json.toPath(), Lists.newArrayList(JsonFormat.printer().print(builder)), UTF_8);
    return isNew;
}

From source file:org.eclipse.che.api.fs.server.impl.FsOperations.java

void delete(Path fsPath) throws ServerException {
    try {//from  ww  w .j  av  a2 s  .  c o m
        if (Files.isDirectory(fsPath)) {
            FileUtils.deleteDirectory(fsPath.toFile());
        } else {
            Files.delete(fsPath);
        }
    } catch (IOException e) {
        throw new ServerException("Failed to delete item: " + fsPath, e);
    }
}

From source file:backtype.storm.localizer.LocalizedResourceRetentionSet.java

protected boolean deleteResource(LocalizedResource resource) {
    try {/*from w ww. j a  v a  2  s.  c  o m*/
        String fileWithVersion = resource.getFilePathWithVersion();
        String currentSymlinkName = resource.getCurrentSymlinkPath();
        String versionFile = resource.getVersionFilePath();
        File deletePath = new File(fileWithVersion);
        if (resource.isUncompressed()) {
            // this doesn't follow symlinks, which is what we want
            FileUtils.deleteDirectory(deletePath);
        } else {
            Files.delete(deletePath.toPath());
        }
        Files.delete(new File(currentSymlinkName).toPath());
        Files.delete(new File(versionFile).toPath());
        return true;
    } catch (IOException e) {
        LOG.warn("Could not delete: {}", resource.getFilePath());
    }
    return false;
}

From source file:org.ballerinalang.composer.service.workspace.local.LocalFSWorkspace.java

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

From source file:cc.kave.commons.externalserializationtests.ExternalTestCaseProviderTest.java

@Test
public void expectedFormattedFileShouldBeOptional() throws IOException, ClassNotFoundException {
    Files.delete(expectedFormattedFile);
    TestCase firstTestCase = getFirstTestCase();
    assertNull(firstTestCase.expectedFormatted);
}

From source file:ch.puzzle.itc.mobiliar.business.utils.SecureFileLoaderTest.java

@Test
public void testIsFileLocatedInDirectorySymbolicLinkFromOutsideOk() throws IOException {
    //symlinks work only on unix
    Assume.assumeTrue(isUnix());//from w w  w .  j ava2s  . c  o m
    Path symlink = Paths.get(FileUtils.getTempDirectoryPath(), "symlink.txt");
    //TODO check: is this statement true? I can't think of any harm this could do...
    //We create a symbolic link outside of the permitted folder pointing to a file inside of the permitted folder. This should be ok as well.
    Files.createSymbolicLink(symlink, f);
    Assert.assertTrue(fileLoader.isFileLocatedInDirectory(dir, symlink));
    Files.delete(symlink);
}

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

/**
 * Test method for {@link org.carcv.impl.core.io.FFMPEGVideoHandler#splitIntoFrames(java.nio.file.Path, int)}.
 *
 * @throws IOException//w w  w  .jav a2 s . com
 */
@Test
public void testSplitIntoFramesPathInt() throws IOException {
    FFMPEGVideoHandler fvh = new FFMPEGVideoHandler();
    FFMPEGVideoHandler.copyCarImagesToDir(entry.getCarImages(), videoDir);
    Path video = fvh.generateVideo(videoDir, FFMPEGVideoHandler.defaultFrameRate);

    assertTrue("Split failed.", fvh.splitIntoFrames(video, FFMPEGVideoHandler.defaultFrameRate));

    Path dir = Paths.get(video.toString() + ".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);
}

From source file:org.fim.command.DetectCorruptionCommandTest.java

private void simulateHardwareCorruption(String fileName) throws IOException {
    Path file = rootDir.resolve(fileName);
    // Keep original timestamps
    BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);

    // A zero byte appears in the middle of the file
    byte[] bytes = Files.readAllBytes(file);
    bytes[bytes.length / 2] = 0;//from   w  w w  .  java2  s.co m

    Files.delete(file);
    Files.write(file, bytes, CREATE);

    // Restore the original timestamps
    Files.getFileAttributeView(file, BasicFileAttributeView.class).setTimes(attributes.lastModifiedTime(),
            attributes.lastAccessTime(), attributes.creationTime());
}

From source file:FileGameAccess.java

@Override
public boolean deleteGameCommands(int gameId) {
    File currentCommandFile = new File(System.getProperty("user.dir") + File.separator + gameId + ".commands");
    boolean exists = currentCommandFile.exists();
    try {/*from  ww w. j  av  a 2 s . com*/
        if (exists)
            Files.delete(currentCommandFile.toPath());
    } catch (IOException ex) {
        Logger.getLogger(FileGameAccess.class.getName()).log(Level.SEVERE, null, ex);
    }
    //boolean delete = currentCommandFile.delete();
    return exists;
}