Example usage for java.nio.file FileSystem getPath

List of usage examples for java.nio.file FileSystem getPath

Introduction

In this page you can find the example usage for java.nio.file FileSystem getPath.

Prototype

public abstract Path getPath(String first, String... more);

Source Link

Document

Converts a path string, or a sequence of strings that when joined form a path string, to a Path .

Usage

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

public static String getFilePathName(String first, String... more) {
    FileSystem fileSystem = FileSystems.getDefault();

    Path filePath = fileSystem.getPath(first, more);

    return filePath.toString();
}

From source file:org.wso2.msf4j.ballerina.Application.java

private static void unzip(String zipFilePath, String destDir) throws IOException {
    BufferedInputStream bis = null;
    FileOutputStream fileOutput = null;
    try (ZipFile file = new ZipFile(zipFilePath)) {
        FileSystem fileSystem = FileSystems.getDefault();
        Enumeration<? extends ZipEntry> entries = file.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                Files.createDirectories(fileSystem.getPath(destDir, entry.getName()));
            } else {
                InputStream is = file.getInputStream(entry);
                bis = new BufferedInputStream(is);
                Path uncompressedFilePath = fileSystem.getPath(destDir, entry.getName());
                Files.createFile(uncompressedFilePath);
                fileOutput = new FileOutputStream(uncompressedFilePath.toString());
                while (bis.available() > 0) {
                    fileOutput.write(bis.read());
                }/*  ww  w.  j av a 2 s.  co  m*/
                fileOutput.close();
            }
        }
    } finally {
        if (bis != null) {
            IOUtils.closeQuietly(bis);
        }
        if (fileOutput != null) {
            IOUtils.closeQuietly(fileOutput);
        }
    }
}

From source file:org.schedulesdirect.grabber.ScheduleTask.java

static void commit(FileSystem vfs) throws IOException {
    Iterator<String> itr = FULL_SCHEDS.keySet().iterator();
    while (itr.hasNext()) {
        String id = itr.next();// ww  w  .j  a v  a2s  .c  o m
        JSONObject sched = new JSONObject();
        List<JSONObject> airs = FULL_SCHEDS.get(id);
        Collections.sort(airs, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject arg0, JSONObject arg1) {
                return arg0.getString("airDateTime").compareTo(arg1.getString("airDateTime"));
            }
        });
        sched.put("programs", airs);
        Path p = vfs.getPath("schedules", String.format("%s.txt", id));
        Files.write(p, sched.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET), StandardOpenOption.WRITE,
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
    }
    FULL_SCHEDS.clear();
}

From source file:com.netflix.spinnaker.halyard.config.services.v1.GenerateService.java

public void generateConfig(NodeReference nodeReference) {
    for (SpinnakerComponent component : spinnakerComponents) {
        FileSystem defaultFileSystem = FileSystems.getDefault();
        AtomicFileWriter writer = null;/*w  w  w  . ja v  a  2s .c  o  m*/
        Path path = defaultFileSystem.getPath(spinnakerOutputPath, component.getConfigFileName());

        log.info("Writing profile to  " + path);

        try {
            writer = new AtomicFileWriter(path);
            writer.write(component.getFullConfig(nodeReference));
            writer.commit();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            throw new HalconfigException(
                    new ProblemBuilder(Problem.Severity.FATAL, "Failed to write config for component "
                            + component.getComponentName() + ": " + ioe.getMessage()).build());
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}

From source file:com.netflix.spinnaker.halyard.config.config.v1.AtomicFileWriter.java

public AtomicFileWriter(Path path) throws IOException {
    FileSystem defaultFileSystem = FileSystems.getDefault();
    this.path = path;
    path.getParent().toFile().mkdir();/*  w  w  w  . java 2 s. c  o m*/
    String tmpDir = System.getProperty("java.io.tmpdir");
    this.tmpPath = defaultFileSystem.getPath(tmpDir, UUID.randomUUID().toString());
    this.writer = Files.newBufferedWriter(this.tmpPath, UTF_8, WRITE, APPEND, CREATE);
}

From source file:business.services.FileService.java

public InputStream getInputStream(File attachment) {
    if (attachment == null) {
        throw new FileNotFound();
    }//  w w w . java2s.com
    try {
        FileSystem fileSystem = FileSystems.getDefault();
        Path path = fileSystem.getPath(uploadPath, attachment.getFilename());
        InputStream input = new FileInputStream(path.toFile());
        return input;
    } catch (IOException e) {
        log.error(e);
        throw new FileDownloadError();
    }
}

From source file:business.services.FileService.java

public HttpEntity<InputStreamResource> download(Long id) {
    try {//from  www.j a v  a2  s. co m
        File attachment = fileRepository.findOne(id);
        if (attachment == null) {
            throw new FileNotFound();
        }
        FileSystem fileSystem = FileSystems.getDefault();
        Path path = fileSystem.getPath(uploadPath, attachment.getFilename());
        InputStream input = new FileInputStream(path.toFile());
        InputStreamResource resource = new InputStreamResource(input);
        HttpHeaders headers = new HttpHeaders();
        if (attachment.getMimeType() != null) {
            headers.setContentType(MediaType.valueOf(attachment.getMimeType()));
        } else {
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        }
        headers.set("Content-Disposition", "attachment; filename=" + attachment.getName().replace(" ", "_"));
        HttpEntity<InputStreamResource> response = new HttpEntity<InputStreamResource>(resource, headers);
        return response;
    } catch (IOException e) {
        log.error(e);
        throw new FileDownloadError();
    }
}

From source file:business.services.FileService.java

public File clone(File file) {
    try {//w  ww  .j av  a  2  s. c om
        FileSystem fileSystem = FileSystems.getDefault();
        // source
        Path source = fileSystem.getPath(uploadPath, file.getFilename());
        // target
        Path path = fileSystem.getPath(uploadPath).normalize();
        String prefix = getBasename(file.getFilename());
        String suffix = getExtension(file.getFilename());
        Path target = Files.createTempFile(path, prefix, suffix).normalize();
        // copy
        log.info("Copying " + source + " to " + target + " ...");
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
        // save clone
        File result = file.clone();
        result.setFilename(target.getFileName().toString());
        return save(result);
    } catch (IOException e) {
        log.error(e);
        throw new FileCopyError();
    }
}

From source file:org.schedulesdirect.grabber.LogoTaskTest.java

@Test
public void testRemovalOfStaleLogosFromZip() throws Exception {
    TestEpgFile epg = new TestEpgFile();
    FileSystem vfs = epg.getFileSystem();
    String callsign = epg.getRandomLogoCallsign();

    JSONObject req = new JSONObject();
    req.put("callsign", callsign);
    req.put("logo", generateLogo(callsign));
    JSONObject cache = new JSONObject();
    cache.put(epg.getRandomLogoCallsign(), "ABC");

    LogoTask task = new LogoTask(req, vfs, cache);
    Path p = vfs.getPath("logos", String.format("%s.png", callsign));
    assertTrue(Files.exists(p));/*from w ww.jav a  2  s.c o  m*/
    task.removeStaleLogo();
    assertFalse(Files.exists(p));
}

From source file:org.polago.deployconf.DeployConfRunner.java

/**
 * Gets the Path to use for storing the DeploymentConfig.
 * <p>// ww w . jav a 2  s.c o  m
 * Unless an explicit deployment config File is set, the File is created in the configured repository and based on
 * the config name. If no repository is set, the current working directory is used.
 *
 * @param name the DeploymentConfig name to use
 * @return the Path to the DeploymentConfig
 */
public Path getDeploymentConfigPath(String name) {
    Path result = deploymentConfigFile;
    String dir = getRepositoryDirectory();
    if (dir == null) {
        dir = "";
    }
    if (deploymentConfigFile != null) {
        result = deploymentConfigFile;
    } else {
        FileSystem fs = FileSystems.getDefault();
        if (name != null) {
            result = fs.getPath(dir, name + "-" + DEPLOYMENT_CONFIG_SUFFIX);
        } else {
            result = fs.getPath(dir, DEPLOYMENT_CONFIG_SUFFIX);
        }
    }

    return result;
}