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:Main.java

/**
 * <p>Decompresses a the content of a file from {@code startPosition} of {@code compressedSize}
 * and return the bytes of it.</p>
 *
 * @param file           the file/*from   www . java  2  s . c  om*/
 * @param startPosition  the start position
 * @param compressedSize the compresse
 * @return the decompressed bytes
 * @throws IOException         if there was any io issues
 * @throws DataFormatException if there was an issue decompressing content
 */
public static byte[] decompress(File file, int startPosition, int compressedSize)
        throws IOException, DataFormatException {

    try (FileChannel fileChannel = FileChannel.open(file.toPath())) {
        ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startPosition, compressedSize);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Inflater inflater = new Inflater();
        byte[] compressedBytes = new byte[compressedSize];
        byte[] inflated = new byte[BUFSIZ];
        int read;

        buffer.get(compressedBytes);
        inflater.setInput(compressedBytes);

        // unzip contents
        while ((read = inflater.inflate(inflated)) != 0) {
            baos.write(inflated, 0, read);
        }

        inflater.end();

        return baos.toByteArray();
    }
}

From source file:ch.devmine.javaparser.Main.java

private static void parseAsDirectory(Project project, HashMap<String, Package> packs, List<Language> languages,
        Language language, String arg) {
    File repoRoot = new File(arg);
    Path repoPath = repoRoot.toPath();

    FilesWalker fileWalker = new FilesWalker();
    try {//from w w w  . j a va  2  s.  com
        Files.walkFileTree(repoPath, fileWalker);
    } catch (IOException ex) {
        Log.e(TAG, ex.getMessage());
    }

    String path = repoPath.toString();
    String name = path.substring(path.lastIndexOf("/") + 1);
    project.setName(name);

    for (String dir : fileWalker.getDirectories()) {
        dir = dir.replaceAll("\n", "");
        if (FileTypeFinder.search(dir, ".java")) {
            Package pack = new Package();
            String packName = dir.substring(dir.lastIndexOf("/") + 1);
            pack.setName(packName);
            pack.setPath(dir);
            packs.put(packName, pack);
        }
    }

    List<String> javaFiles = FileWalkerUtils.extractJavaFiles(fileWalker.getRegularFiles());
    for (String javaFile : javaFiles) {
        try {
            Parser parser = new Parser(javaFile, null);
            SourceFile sourceFile = new SourceFile();
            String packName = FileWalkerUtils.extractFolderName(javaFile);
            parseAndFillSourceFile(parser, sourceFile, javaFile, language, packs, packName);
        } catch (FileNotFoundException ex) {
            Log.e(TAG, ex.getMessage());
        }
    }

}

From source file:controler.util.FileUtil.java

public static void upload(UploadedFile file, String emplacement, Abonne abonne, File f) throws IOException {
    System.out.println("upload");
    //String vmParam = "irisi.projet.upload.path";
    String fullPath = "";
    if (file != null) {
        String path = System.getProperty(vmParam);
        if (path == null) {
            JsfUtil.addErrorMessage(null, "option JVM manquante \"" + vmParam + "\"");
        } else {//from www  .  j a va 2  s .c om
            File folder = new File(
                    path + "\\" + abonne.getNom() + "XX" + abonne.getId() + "\\" + emplacement + "\\");//creer la path qui va contenir notre fichier
            if (!folder.exists()) {
                folder.mkdirs(); // Cration de l'arborescense (dossier et sous dossier)
            }
            System.out.println("file.getFileName() ==> " + file.getFileName());
            String nameModified = file.getFileName().replace('.', ':');
            String[] str = nameModified.split(":");
            String fileName = str[0];
            String extension = str[1];
            String outputPath = path + "\\" + abonne.getNom() + "XX" + abonne.getId() + "\\" + emplacement
                    + "\\" + fileName + new Date().getTime() + "." + extension;//hna fin kaytht l fichier selectionn
            System.out.println(outputPath);
            File outputFile = new File(outputPath);
            Files.copy(file.getInputstream(), outputFile.toPath());
            JsfUtil.addSuccessMessage(file.getFileName() + " est bien recu.");
        }
    }
}

From source file:controler.util.FileUtil.java

public static String uploadV2(UploadedFile file, String emplacement, Abonne abonne) throws IOException {
    System.out.println("upload");
    //String vmParam = "irisi.projet.upload.path";
    String fullPath = "";
    if (file != null) {
        String path = System.getProperty(vmParam);
        if (path == null) {
            JsfUtil.addErrorMessage(null, "option JVM manquante \"" + vmParam + "\"");
        } else {//ww  w.  j  a  v a 2s . c o m
            File folder = new File(abonne.getCheminDossier() + "\\" + emplacement + "\\");//creer la path qui va contenir notre fichier
            if (!folder.exists()) {
                folder.mkdirs(); // Cration de l'arborescense (dossier et sous dossier)
            }
            System.out.println("file.getFileName() ==> " + file.getFileName());
            String nameModified = file.getFileName().replace('.', ':');
            String[] str = nameModified.split(":");
            String fileName = str[0];
            String extension = str[1];
            String outputPath = abonne.getCheminDossier() + "\\" + emplacement + "\\" + fileName
                    + new Date().getTime() + "." + extension;//hna fin kaytht l fichier selectionn
            System.out.println(outputPath);
            File outputFile = new File(outputPath);
            Files.copy(file.getInputstream(), outputFile.toPath());
            JsfUtil.addSuccessMessage(file.getFileName() + " est bien recu.");
            return outputPath;
        }
    }
    return "";
}

From source file:Main.StaticTools.java

public static File getFile(File dir) {
    FileChooser chooser = new FileChooser();
    if (Files.exists(dir.toPath()))
        chooser.setInitialDirectory(dir);
    else//  www . j  av a 2  s .c  o  m
        chooser.setInitialDirectory(new File("C:\\"));
    return chooser.showSaveDialog(null);
}

From source file:com.huawei.streaming.cql.CQLTestCommons.java

/**
 * ??//from w  w w  . j a  v a 2 s.  c om
 *
 * @param f1 1
 * @param f2 2
 * @return ?true
 * @throws IOException ?
 */
public static boolean compareFileContent(File f1, File f2) throws IOException {
    List<String> f1Contexts = Files.readAllLines(f1.toPath(), CHARSET);
    List<String> f2Contexts = Files.readAllLines(f2.toPath(), CHARSET);

    if (f1Contexts.size() != f2Contexts.size()) {
        return false;
    }
    for (int i = 0; i < f1Contexts.size(); i++) {
        String s1 = f1Contexts.get(i).trim();
        String s2 = f2Contexts.get(i).trim();
        if (Strings.isNullOrEmpty(s1) && Strings.isNullOrEmpty(s2)) {
            continue;
        }

        if (!s1.equals(s2)) {
            return false;
        }
    }
    return true;
}

From source file:io.github.robolib.util.mapper.RobotMap.java

public static void setMapFile(String file) {
    m_mapFile = file;/*w  w w  .j a  va2 s.c  o  m*/
    m_enabled = true;
    File f = new File(m_mapFile);
    try {
        m_jMap = new JSONObject(new JSONTokener(Files.newBufferedReader(f.toPath())));
    } catch (JSONException | IOException e) {
        Logger.get(RobotMap.class).fatal("Failed to load config file", e);
    }
}

From source file:com.dragome.web.helpers.serverside.DragomeCompilerLauncher.java

private static Classpath process(Classpath classPath, DragomeConfigurator configurator) {
    try {/* w  ww . j  a  v  a  2s  .c om*/
        String path = null;

        String tempDir = System.getProperty("java.io.tmpdir");
        File tmpDir = new File(tempDir + File.separatorChar + "dragomeTemp");
        Path tmpPath = tmpDir.toPath();
        FileUtils.deleteDirectory(tmpDir);
        Files.createDirectories(tmpPath);
        File file = Files.createTempFile(tmpPath, "dragome-merged-", ".jar").toFile();
        file.deleteOnExit();
        path = file.getAbsolutePath();

        try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(file))) {
            List<ClasspathEntry> entries = classPath.getEntries();
            for (ClasspathEntry classpathEntry : entries)
                classpathEntry.copyFilesToJar(jos, new DefaultClasspathFileFilter() {
                    private ArrayList<String> keepClass = new ArrayList<>();

                    public boolean accept(ClasspathFile classpathFile) {
                        boolean result = super.accept(classpathFile);

                        String entryName = classpathFile.getPath();
                        if (!keepClass.contains(entryName)) {
                            keepClass.add(entryName);

                            if (entryName.endsWith(".js") || entryName.endsWith(".class")
                                    || entryName.contains("MANIFEST") || entryName.contains(".html")
                                    || entryName.contains(".css"))
                                result &= true;
                        }
                        return result;
                    }
                });
        }

        if (configurator.isRemoveUnusedCode()) {
            return runProguard(file, configurator);
        } else
            return new Classpath(JarClasspathEntry.createFromPath(path));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.text.TokenizedTextWriter.java

/**
 * Read a file containing stopwords (one per line).
 * <p>//from  w  w w  .  j a va2 s.c  o  m
 * Empty lines and lines starting with ("#") are filtered out.
 * 
 * @param f
 * @return
 * @throws IOException
 */
private static Set<String> readStopwordsFile(File f) throws IOException {
    return Files.readAllLines(f.toPath()).stream().map(String::trim).filter(l -> !l.isEmpty())
            .filter(l -> !l.startsWith("#")).map(w -> w.toLowerCase()).collect(Collectors.toSet());
}

From source file:com.esofthead.mycollab.core.utils.FileUtils.java

public static void mkdirs(File file) {
    if (file.exists()) {
        return;//from   w ww . j a  v a2s  . co  m
    }
    try {
        Files.createDirectories(file.toPath());
    } catch (IOException e) {
        throw new MyCollabException(e);
    }
}