Example usage for java.io File toPath

List of usage examples for java.io File toPath

Introduction

In this page you can find the example usage for java.io File toPath.

Prototype

public Path toPath() 

Source Link

Document

Returns a Path java.nio.file.Path object constructed from this abstract path.

Usage

From source file:org.fon.documentmanagementsystem.controllers.DocumentController.java

@RequestMapping(path = "/download/{id}", method = RequestMethod.GET)
public ResponseEntity<byte[]> downloadFile(@PathVariable("id") long id) {
    try {/*ww  w  . j  a  v  a  2  s  . co  m*/

        Dokument document = dokumentService.findOne(id);

        HttpHeaders header = new HttpHeaders();
        //header.setContentType(MediaType.valueOf(document.getFajlTip()));

        String nazivfajla = document.getFajl();
        int li = nazivfajla.lastIndexOf('\\');
        String subsnaziv = nazivfajla.substring(li + 1, nazivfajla.length());
        header.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + subsnaziv);
        File file = new File(nazivfajla);

        Path path = file.toPath();

        byte[] outputByte = Files.readAllBytes(path);

        String fajltype = Files.probeContentType(path);
        System.out.println(fajltype + " je tip");

        header.setContentType(MediaType.valueOf(fajltype));

        header.setContentLength(outputByte.length);

        return new ResponseEntity<>(outputByte, header, HttpStatus.OK);
    } catch (Exception e) {
        return null;
    }
}

From source file:org.trellisldp.rosid.file.CachedResource.java

@Override
public Stream<Quad> stream() {
    final File file = new File(directory, RESOURCE_QUADS);
    if (file.exists()) {
        try {/* w ww  .  jav  a 2 s. c o m*/
            return lines(file.toPath()).map(line -> stringToQuad(rdf, line)).filter(Optional::isPresent)
                    .map(Optional::get);
        } catch (final IOException ex) {
            LOGGER.warn("Could not read file at {}: {}", file, ex.getMessage());
        }
    }
    return empty();
}

From source file:com.offbynull.coroutines.antplugin.InstrumentTask.java

private void instrumentPath(Instrumenter instrumenter) throws IOException {
    for (File inputFile : FileUtils.listFiles(sourceDirectory, new String[] { "class" }, true)) {
        Path relativePath = sourceDirectory.toPath().relativize(inputFile.toPath());
        Path outputFilePath = targetDirectory.toPath().resolve(relativePath);
        File outputFile = outputFilePath.toFile();

        log("Instrumenting " + inputFile, Project.MSG_INFO);
        byte[] input = FileUtils.readFileToByteArray(inputFile);
        byte[] output = instrumenter.instrument(input);
        log("File size changed from " + input.length + " to " + output.length, Project.MSG_DEBUG);
        FileUtils.writeByteArrayToFile(outputFile, output);
    }//from  w ww . j  a va2 s .c  o  m
}

From source file:com.osbitools.ws.shared.prj.web.EntityUtilsMgrWsSrvServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {/*from ww w  .  j  a v  a2  s .c o m*/
        super.doGet(req, resp);
    } catch (ServletException e) {
        if (e.getCause().getClass().equals(WsSrvException.class)) {
            // Authentication failed
            checkSendError(req, resp, (WsSrvException) e.getCause());
            return;
        } else {
            throw e;
        }
    }

    // Check if only info requires
    boolean fcheck = req.getParameter("check") != null;
    String name = req.getParameter(PrjMgrConstants.REQ_NAME_PARAM);

    if (fcheck) {
        try {
            // Get info about single file
            printJson(resp, GenericUtils.getInfo(getPrjRootDir(req), name, getBaseExt(req),
                    getReqParamValues(req), isMinfied(req)));
        } catch (WsSrvException e) {
            return;
        }
    } else {
        try {
            // Download file
            File f = GenericUtils.checkFile(getPrjRootDir(req), name, getBaseExt(req));
            printMimeFile(resp, Files.probeContentType(f.toPath()), name, GenericUtils.readFile(f), "utf-8");
        } catch (WsSrvException e) {
            checkSendError(req, resp, e);
        }
    }
}

From source file:io.mangoo.maven.MangooMojo.java

private void getArtifacts(Set<String> includesSet, Set<String> excludesSet, Set<Path> watchDirectories) {
    for (Artifact artifact : project.getArtifacts()) {
        File file = artifact.getFile();

        if (file.isDirectory() && this.watchAllClassPathDirs) {
            watchDirectories.add(file.toPath().toAbsolutePath());
        } else if (file.getName().endsWith(".jar") && this.watchAllClassPathJars) {
            File parentDir = file.getParentFile();
            Path parentPath = parentDir.toPath().toAbsolutePath();

            String rulePrefix = parentDir.getAbsolutePath() + File.separator;
            rulePrefix = rulePrefix.replace("\\", "\\\\");

            if (!watchDirectories.contains(parentPath)) {
                excludesSet.add(rulePrefix + "(.*)$");
            }/*from ww w .  j a v a  2 s.c o  m*/

            includesSet.add(rulePrefix + file.getName() + "$");
            watchDirectories.add(parentPath);
        }
    }
}

From source file:com.sonar.it.java.suite.SonarLintTest.java

private ClientInputFile prepareInputFile(String relativePath, String content, final boolean isTest)
        throws IOException {
    final File file = new File(baseDir, relativePath);
    FileUtils.write(file, content, StandardCharsets.UTF_8);
    return createInputFile(file.toPath(), isTest);
}

From source file:halive.shootinoutside.common.core.game.map.GameMap.java

public void saveMapToFile(File f) throws IOException {
    Files.write(f.toPath(), serializeMapToCompressedByteArray());
}

From source file:org.italiangrid.storm.webdav.fs.attrs.DefaultExtendedFileAttributesHelper.java

@Override
public void setExtendedFileAttribute(File f, String attributeName, String attributeValue) throws IOException {

    Assert.notNull(f);//from  w w w  .ja va  2 s.  c  om
    Assert.hasText(attributeName);

    UserDefinedFileAttributeView faView = Files.getFileAttributeView(f.toPath(),
            UserDefinedFileAttributeView.class);

    if (faView == null) {
        throw new IOException("UserDefinedFileAttributeView not supported on file " + f.getAbsolutePath());
    }

    faView.write(attributeName, StandardCharsets.UTF_8.encode(attributeValue));
}

From source file:com.hurence.logisland.processor.CsvLoaderTest.java

@Test
public void CsvLoaderUnparsableExceptionTest() throws IOException {
    File f = new File(RESOURCES_DIRECTORY + "artificialWithAnomaly/art_daily_flatmiddle.csv");

    DateTimeFormatter wrongInputDateFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH-mm-ss");

    BufferedReader reader = Files.newBufferedReader(f.toPath(), ENCODING);
    try {/*from w w w  .j a va2s  .  c om*/
        TimeSeriesCsvLoader.load(reader, true, wrongInputDateFormat);
    } catch (RuntimeException e) {
        return;
    }

    Assert.fail("Should have failed with an UnparsableException exception");
}

From source file:JDBCExecutor.java

public void executeSQLFile(File sqlFile) {
    Preconditions.checkArgument(sqlFile.exists() && sqlFile.isFile(), "Please provide valid file.." + sqlFile);
    try {//from   w ww .  ja  va2  s.  c  o m
        List<String> sqlLines = Files.readAllLines(sqlFile.toPath());
        for (String sql : sqlLines) {
            sql = sql.trim();
            if (sql.endsWith(";")) {
                sql = sql.substring(0, sql.lastIndexOf(";"));
            }
            if (!sql.isEmpty()) {
                executeStatement(sql);
                System.out.println();
            }
        }
    } catch (IOException e) {
        LOG("Error in processing file " + sqlFile, e);
    }
}