Example usage for java.nio.file Path toFile

List of usage examples for java.nio.file Path toFile

Introduction

In this page you can find the example usage for java.nio.file Path toFile.

Prototype

default File toFile() 

Source Link

Document

Returns a File object representing this path.

Usage

From source file:ch.admin.suis.msghandler.common.MessageCollection.java

private Message fetchDataFile(final String suffix, Message message, File envelope)
        throws IllegalStateException {
    DirectoryStream<Path> fileStream = FileUtils.listFiles(new File(messageDir.getAbsolutePath()),
            new DirectoryStream.Filter<Path>() {
                public boolean accept(Path pathname) throws IOException {
                    return pathname.toFile().getName().startsWith("data_" + suffix + ".")
                            && FileFilters.isReadableFile(pathname);
                }//from w ww  .  ja  v a  2 s.c  om
            });
    List<File> files = directoryStreamToListOfFiles(fileStream);
    if (files.isEmpty()) { // No data files detected
        throw new IllegalStateException(
                "Cannot find the data file for the envelope " + envelope.getAbsolutePath());
    } else if (files.size() > 1) { // Too many data files detected
        throw new IllegalStateException("Several data files have been found corresponding with the suffix "
                + suffix + ". This is never supposed to happen.");
    } else { // One data files detected, good news
        message.setDataFile(files.get(0));
        LOG.info(MessageFormat.format("reading the data files {0} for the message ID {1}",
                message.getDataFile().getAbsolutePath(), message.getMessageId()));

    }

    return message;
}

From source file:io.mangoo.build.Watcher.java

public void handleNewOrModifiedFile(Path path) {
    String absolutePath = path.toFile().getAbsolutePath();
    if (isPreprocess(absolutePath)) {
        MinificationUtils.preprocess(absolutePath);
        String[] tempPath = absolutePath.split("files");
        MinificationUtils.minify(tempPath[0] + "files/assets/stylesheet/"
                + StringUtils.substringAfterLast(absolutePath, "/")
                        .replace(Suffix.SASS.toString(), Suffix.CSS.toString())
                        .replace(Suffix.LESS.toString(), Suffix.CSS.toString()));
    }//  www.  j av a2s. c o  m

    if (isAsset(absolutePath)) {
        MinificationUtils.minify(absolutePath);
    }

    RuleMatch match = matchRule(includes, excludes, absolutePath);
    if (match.proceed) {
        this.trigger.trigger();
    }
}

From source file:org.apache.jena.osgi.test.JenaOSGITest.java

@Test
public void testJenaArq() throws Exception {
    Dataset dataset = DatasetFactory.createMem();
    dataset.addNamedModel(EXAMPLE_COM_GRAPH, makeModel());

    Path path = Files.createTempFile("example", ".jsonld");
    // System.out.println(path);
    path.toFile().deleteOnExit();

    try (OutputStream output = Files.newOutputStream(path)) {
        RDFDataMgr.write(output, dataset, Lang.JSONLD);
    }//  ww  w .j a  v a  2s .  c  o  m
    // We test JSON-LD as it involves multiple other bundles

    Dataset dataset2 = RDFDataMgr.loadDataset(path.toUri().toString());
    assertTrue(dataset2.containsNamedModel(EXAMPLE_COM_GRAPH));

    runQuery(dataset2);

}

From source file:com.streamsets.pipeline.lib.io.LiveFile.java

/**
 * Refreshes the <code>LiveFile</code>, if the file was renamed, the path will have the new name.
 *
 * @return the refreshed file if the file has been renamed, or itself if the file has not been rename or the file
 * does not exist in the directory anymore.
 * @throws IOException thrown if the LiveFile could not be refreshed
 *//* ww w  . j ava 2 s  . c om*/
public LiveFile refresh() throws IOException {
    LiveFile refresh = this;
    boolean changed;
    try {
        BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
        String iNodeCurrent = attrs.fileKey().toString();
        int headLenCurrent = (int) Math.min(headLen, attrs.size());
        String headHashCurrent = computeHash(path, headLenCurrent);
        changed = !this.iNode.equals(iNodeCurrent) || !this.headHash.equals(headHashCurrent);
    } catch (NoSuchFileException ex) {
        changed = true;
    }
    if (changed) {

        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path.getParent())) {
            for (Path path : directoryStream) {
                if (path.toFile().isDirectory()) {
                    continue;
                }
                BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
                String iNode = attrs.fileKey().toString();
                int headLen = (int) Math.min(this.headLen, attrs.size());
                String headHash = computeHash(path, headLen);
                if (iNode.equals(this.iNode) && headHash.equals(this.headHash)) {
                    if (headLen == 0) {
                        headLen = (int) Math.min(HEAD_LEN, attrs.size());
                        headHash = computeHash(path, headLen);
                    }
                    return new LiveFile(path, iNode, headHash, headLen);
                } /**rename??*/
            }
        }
        return null;
    } /**change? itself*/
    return refresh;
}

From source file:de.learnlib.alex.data.dao.FileDAOImpl.java

@Override
public String getAbsoluteFilePath(User user, Long projectId, String fileName) throws NotFoundException {
    projectDAO.getByID(user.getId(), projectId); // access check

    File uploadDirectory = getUploadDirectory(user, projectId);

    Path uploadedFileLocation = Paths.get(uploadDirectory.getPath(), fileName);
    File file = uploadedFileLocation.toFile();

    if (!file.exists()) {
        throw new NotFoundException("Could not find the file in the project.");
    }/* w w  w.  j a  va2  s. c o m*/

    return file.getAbsolutePath();
}

From source file:de.learnlib.alex.data.dao.FileDAOImpl.java

@Override
public void delete(User user, Long projectId, String fileName) throws NotFoundException {
    projectDAO.getByID(user.getId(), projectId); // access check

    File uploadDirectory = getUploadDirectory(user, projectId);

    Path uploadedFileLocation = Paths.get(uploadDirectory.getPath(), fileName);
    File file = uploadedFileLocation.toFile();

    if (!file.exists()) {
        throw new NotFoundException("Could not find the file in the project.");
    }// w  w  w.  ja v a2s.c  o  m

    file.delete();
}

From source file:com.kumarvv.setl.Setl.java

/**
 * load dataSources from default db.json file when def file skips them
 *
 * @param def//from  w w w.ja v  a2  s.  com
 */
protected void loadDataStores(final Def def) {
    if (def == null || def.getFilePath() == null) {
        return;
    }

    Path dbPath = def.getFilePath().resolveSibling("db.json");
    if (dbPath == null || !dbPath.toFile().exists()) {
        // resolve from parent folders
        Path parent = def.getFilePath().getParent();
        while (parent != null) {
            dbPath = parent.resolveSibling("db.json");
            if (dbPath != null && dbPath.toFile().exists()) {
                break;
            }
            parent = parent.getParent();
        }
        if (dbPath == null || !dbPath.toFile().exists()) {
            return;
        }
    }

    try {
        Def dbDef = new ObjectMapper().readValue(dbPath.toFile(), Def.class);

        if (def.getFromDS() == null && dbDef.getFromDS() != null) {
            def.setFromDS(new DS());
            def.getFromDS().setUrl(dbDef.getFromDS().getUrl());
            def.getFromDS().setUsername(dbDef.getFromDS().getUsername());
            def.getFromDS().setPassword(dbDef.getFromDS().getPassword());
        }
        if (def.getToDS() == null && dbDef.getToDS() != null) {
            def.setToDS(new DS());
            def.getToDS().setUrl(dbDef.getToDS().getUrl());
            def.getToDS().setUsername(dbDef.getToDS().getUsername());
            def.getToDS().setPassword(dbDef.getToDS().getPassword());
        }
    } catch (Exception e) {
        Logger.error("DB.json error: {}", e.getMessage());
        // just ignore
    }
}

From source file:com.qwazr.extractor.ExtractorServiceImpl.java

private String getMimeMagic(Path filePath) {
    try {//www. ja  v a  2  s  . co m
        final MagicMatch match = Magic.getMagicMatch(filePath.toFile(), true, true);
        if (match == null)
            return null;
        return match.getMimeType();
    } catch (MagicParseException | MagicMatchNotFoundException | MagicException e) {
        return null;
    }
}

From source file:com.relicum.ipsum.io.PropertyIO.java

/**
 * Return a {@link java.util.Properties} read from the specified string file path.
 *
 * @param path the {@link java.nio.file.Path } path of the file to read the properties from.
 * @return the {@link java.util.Properties} instance.
 * @throws IOException the {@link java.io.IOException} if the file was not found or there were problems reading from it.
 *//* w w  w.j  av  a 2  s. com*/
default Properties readFromFile(Path path) throws IOException {

    Validate.notNull(path);

    return readFromFile(path.toFile());
}

From source file:net.sf.jabref.logic.xmp.XMPUtil.java

public static Collection<BibEntry> readXMP(Path filePath, JabRefPreferences prefs) throws IOException {
    return readXMP(filePath.toFile(), prefs);
}