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:uk.ac.sanger.cgp.wwdocker.actions.Utils.java

public static List<File> getWorkInis(BaseConfiguration config) {
    List<File> iniFiles = new ArrayList();
    Path dir = Paths.get(config.getString("wfl_inis"));
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path item : stream) {
            File file = item.toFile();
            if (file.isFile() && file.canRead() && !file.isHidden()) {
                if (!file.getName().endsWith(".ini")) {
                    continue;
                }/*  w  w  w  .  ja  v  a 2  s  . c o  m*/
                iniFiles.add(file);
            }

        }
    } catch (IOException | DirectoryIteratorException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return iniFiles;
}

From source file:com.thesmartguild.firmloader.lib.tftp.TFTPServerFactory.java

public static TFTPServer instantiate(File file) {
    try {/* w  w w. java  2 s  .  c  o  m*/
        TFTPServer TFTPServer_;
        Path tmpFilePath = TFTPServerFactory.createTempDirectory();
        Path filePath = Files.copy(file.toPath(), Paths.get(tmpFilePath.toString(), file.getName()),
                StandardCopyOption.REPLACE_EXISTING);
        filePath.toFile().deleteOnExit();
        TFTPServer_ = new TFTPServer(tmpFilePath.toFile(), tmpFilePath.toFile(), ServerMode.GET_ONLY);
        TFTPServer_.setLog(System.out);
        TFTPServer_.setLogError(System.out);
        return TFTPServer_;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:company.gonapps.loghut.utils.FileUtils.java

public static void rmdir(Path directoryPath, DirectoryStream.Filter<Path> ignoringFilter)
        throws NotDirectoryException, IOException {
    if (!directoryPath.toFile().isDirectory())
        throw new NotDirectoryException(directoryPath.toString());

    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directoryPath)) {
        List<Path> ignoredPaths = new LinkedList<>();

        for (Path path : directoryStream) {
            if (!ignoringFilter.accept(path))
                return;
            ignoredPaths.add(path);//from  w w w.  j  a  v  a  2 s.  com
        }

        for (Path ignoredPath : ignoredPaths) {
            Files.delete(ignoredPath);
        }

        Files.delete(directoryPath);
    }
}

From source file:jvmoptions.OptionAnalyzer.java

static Map<String, Map<String, String>> makeMap(String root) throws Exception {
    Path start = FileSystems.getDefault().getPath(root);
    File dir = start.toFile();
    if (dir.exists() == false || dir.isDirectory() == false) {
        System.err.printf("%s doesn't exists or doesn't dir %n", root);
        System.err.println("run gradle task below");
        System.err.println("gradlew getSrcs");
        System.exit(1);//from w  ww .ja v  a 2s  . c o  m
    }

    return Files.walk(start).map(Path::toFile).filter(File::isFile).filter(f -> f.getName().endsWith(".hpp"))
            .map(File::toPath).parallel().filter(OptionAnalyzer::contains).map(OptionAnalyzer::parse)
            .map(Map::entrySet).flatMap(Collection::stream)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, OptionAnalyzer::merge));
}

From source file:com.edduarte.argus.util.PluginLoader.java

private static Stream<Path> filesInDir(Path dir) {
    if (!dir.toFile().exists()) {
        return Stream.empty();
    }//from  w ww . j  av a  2  s .  c o m

    try {
        return Files.list(dir).flatMap(path -> path.toFile().isDirectory() ? filesInDir(path)
                : Collections.singletonList(path).stream());

    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.liferay.sync.engine.util.MSOfficeFileUtil.java

public static Date getLastSavedDate(Path filePath) {
    NPOIFSFileSystem npoifsFileSystem = null;

    try {/*from  ww w  .  j  av a2 s  .co  m*/
        npoifsFileSystem = new NPOIFSFileSystem(filePath.toFile());

        HPSFPropertiesOnlyDocument hpsfPropertiesOnlyDocument = new HPSFPropertiesOnlyDocument(
                npoifsFileSystem);

        SummaryInformation summaryInformation = hpsfPropertiesOnlyDocument.getSummaryInformation();

        return summaryInformation.getLastSaveDateTime();
    } catch (Exception e) {
        return null;
    } finally {
        if (npoifsFileSystem != null) {
            try {
                npoifsFileSystem.close();
            } catch (Exception e) {
                return null;
            }
        }
    }
}

From source file:com.netflix.conductor.dao.es5.es.EmbeddedElasticSearch.java

private static void createDataDir(String dataDirLoc) {
    try {/*from   ww w .j  av a2s. com*/
        Path dataDirPath = FileSystems.getDefault().getPath(dataDirLoc);
        Files.createDirectories(dataDirPath);
        dataDir = dataDirPath.toFile();
    } catch (IOException e) {
        logger.error("Failed to create data dir");
    }
}

From source file:ec.edu.chyc.manejopersonal.managebean.util.BeansUtils.java

/***
 * Abre un stream para descargar un archivo
 * @param direccionArchivoOrigen Archivo a descargar
 * @param nombreArchivoDescarga Nombre del archivo como se quiere descargar, puede ser diferente al nombre original
 * @return Stream de descarga// w  ww.  j a  va  2s. c  o  m
 * @throws FileNotFoundException En caso de no encontrar el archivo
 */
public static StreamedContent streamParaDescarga(Path direccionArchivoOrigen, String nombreArchivoDescarga)
        throws FileNotFoundException {
    String nombreArchivoGuardado = direccionArchivoOrigen.getFileName().toString();
    String extension = FilenameUtils.getExtension(nombreArchivoGuardado);

    String nuevoNombre = ServerUtils.convertirNombreArchivo(nombreArchivoDescarga, extension, 40);

    Path pathArchivo = direccionArchivoOrigen;
    InputStream stream = new BufferedInputStream(new FileInputStream(pathArchivo.toFile()));

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    StreamedContent streamParaDescarga = new DefaultStreamedContent(stream,
            externalContext.getMimeType(nuevoNombre), nuevoNombre);
    return streamParaDescarga;
}

From source file:com.yqboots.fss.util.ZipUtils.java

/**
 * Compresses the specified directory to a zip file
 *
 * @param dir the directory to compress/*from   w w w . java2s . c o  m*/
 * @return the compressed file
 * @throws IOException
 */
public static Path compress(Path dir) throws IOException {
    Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath());
    Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath());

    Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP);
    try (final ZipOutputStream out = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(result.toFile())))) {
        // out.setMethod(ZipOutputStream.DEFLATED);
        final byte data[] = new byte[BUFFER];
        // get a list of files from current directory
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs)
                    throws IOException {
                final File file = path.toFile();

                // compress to relative directory, not absolute
                final String root = StringUtils.substringAfter(file.getParent(), dir.toString());
                try (final BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file),
                        BUFFER)) {
                    final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName());
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                }

                return FileVisitResult.CONTINUE;
            }
        });
    }

    return result;
}

From source file:org.synchronoss.cloud.nio.multipart.example.FileUploadClientIntegrationTest.java

static Collection<TestCase> getTestCases() {
    try {/*from   w  w  w .j  a  v  a2s . co m*/
        URL resourceUrl = FileUploadClientIntegrationTest.class.getResource("/test-files");
        Path resourcePath = Paths.get(resourceUrl.toURI());
        File[] files = resourcePath.toFile().listFiles();

        if (files == null) {
            log.warn("Empty test-files folder");
            return Collections.emptyList();
        }

        int applicationServerPort = Integer.parseInt(System.getProperty("application.server.port", "8080"));
        List<TestCase> testCases = new ArrayList<TestCase>();
        for (File file : files) {
            for (String url : URLS) {
                testCases.add(new TestCase(file, String.format(url, applicationServerPort)));
            }
        }
        return testCases;
    } catch (Exception e) {
        throw new IllegalStateException("Cannot find the test file", e);
    }
}