Example usage for java.nio.file Path normalize

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

Introduction

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

Prototype

Path normalize();

Source Link

Document

Returns a path that is this path with redundant name elements eliminated.

Usage

From source file:Main.java

public static void main(String[] args) {
    Path path = Paths.get("C:/home/./music/users.txt");
    System.out.println("Normalized: " + path.normalize());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path p1 = Paths.get("C:\\Java_Dev\\..\\\\Java_Dev\\test1.txt");
    Path p1n = p1.normalize();
    System.out.println(p1 + "  normalized to " + p1n);

    Path p2 = Paths.get("C:\\Java_Dev\\test1.txt");
    Path p2n = p2.normalize();//  w w  w. ja  v  a2 s  .  com
    System.out.println(p2 + "  normalized to " + p2n);

    Path p3 = Paths.get("\\..\\.\\test.txt");
    Path p3n = p3.normalize();
    System.out.println(p3 + "  normalized to " + p3n);
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path1 = Paths.get("/home/docs/users.txt");
    Path path2 = Paths.get("/home/music/users.txt");

    System.out.println(Files.isSymbolicLink(path1));
    System.out.println(Files.isSymbolicLink(path2));

    Path path = Paths.get(new URI("C:/home/./music/users.txt"));
    System.out.println("Normalized: " + path.normalize());
    System.out.println("Absolute path: " + path.toAbsolutePath());
    System.out.println("URI: " + path.toUri());
    System.out.println("toRealPath (Do not follow links): " + path.toRealPath(LinkOption.NOFOLLOW_LINKS));
    System.out.println("toRealPath: " + path.toRealPath());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get(new URI("C:/home/./music/users.txt"));
    System.out.println(path);/*w ww .  j  a va2s.  c o m*/
    System.out.println("Normalized: " + path.normalize());
}

From source file:Test.java

public static void main(String[] args) {
    Path path = Paths.get("/home/docs/../music/A.mp3");
    System.out.println("Absolute path: " + path.toAbsolutePath());
    System.out.println("URI: " + path.toUri());
    System.out.println("Normalized Path: " + path.normalize());
    System.out.println("Normalized URI: " + path.normalize().toUri());
    System.out.println();/*from  ww  w  .  java2s.c  om*/

    path = Paths.get("/home/./music/A.mp3");
    System.out.println("Absolute path: " + path.toAbsolutePath());
    System.out.println("URI: " + path.toUri());
    System.out.println("Normalized Path: " + path.normalize());
    System.out.println("Normalized URI: " + path.normalize().toUri());

}

From source file:Main.java

public static void main(String[] args) {
    Path p1 = Paths.get("test.txt");
    try {//ww w  . ja va 2 s.  c  o  m
        Files.createFile(p1);
        System.out.format("File created:  %s%n", p1.toRealPath());
    } catch (FileAlreadyExistsException e) {
        System.out.format("File %s  already exists.%n", p1.normalize());
    } catch (NoSuchFileException e) {
        System.out.format("Directory %s  does  not  exists.%n", p1.normalize().getParent());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.swt.snippets.SnippetExplorer.java

/**
 * SnippetExplorer main method.//from  w w  w.  ja v  a 2s. c  om
 *
 * @param args does not parse any arguments
 */
public static void main(String[] args) throws Exception {
    final String os = System.getProperty("os.name");
    multiDisplaySupport = (os != null && os.toLowerCase().contains("windows"));
    if (canRunCommand("java")) {
        javaCommand = "java";
    } else {
        final String javaHome = System.getProperty("java.home");
        if (javaHome != null) {
            final Path java = Paths.get(javaHome, "bin", "java");
            java.normalize();
            if (canRunCommand(java.toString())) {
                javaCommand = java.toString();
            }
        }
    }

    snippets = loadSnippets();
    snippets.sort((a, b) -> {
        int cmp = Integer.compare(a.snippetNum, b.snippetNum);
        if (cmp == 0) {
            cmp = a.snippetName.compareTo(b.snippetName);
        }
        return cmp;
    });

    new SnippetExplorer().open();
}

From source file:org.fim.util.SELinux.java

/**
 * Retrieve the SELinux label of the specified file.
 *///from   w  w w .j  a v a  2 s  .  c om
public static String getLabel(Context context, Path file) {
    String fileName = file.normalize().toAbsolutePath().toString();
    try {
        String line = CommandUtil.executeCommand(Arrays.asList("ls", "-1Z", fileName));
        String[] strings = line.split(" ");
        if (strings.length == 2) {
            return strings[0];
        }
    } catch (Exception ex) {
        Logger.error("Error retrieving SELinux label for '" + file + "'", ex, context.isDisplayStackTrace());
    }

    return null;
}

From source file:org.fim.util.SELinux.java

/**
 * Set the SELinux label of the specified file.
 *//*from w w  w .  j  a v  a 2  s .c o m*/
public static void setLabel(Context context, Path file, String label) {
    String fileName = file.normalize().toAbsolutePath().toString();
    try {
        CommandUtil.executeCommand(Arrays.asList("chcon", label, fileName));
    } catch (Exception ex) {
        Logger.error("Error setting SELinux label for '" + file + "'", ex, context.isDisplayStackTrace());
    }
}

From source file:com.blackducksoftware.integration.hub.detect.util.DetectZipUtil.java

public static void unzip(File zip, File dest, Charset charset) throws IOException {
    Path destPath = dest.toPath();
    try (ZipFile zipFile = new ZipFile(zip, ZipFile.OPEN_READ, charset)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            Path entryPath = destPath.resolve(entry.getName());
            if (!entryPath.normalize().startsWith(dest.toPath()))
                throw new IOException("Zip entry contained path traversal");
            if (entry.isDirectory()) {
                Files.createDirectories(entryPath);
            } else {
                Files.createDirectories(entryPath.getParent());
                try (InputStream in = zipFile.getInputStream(entry)) {
                    try (OutputStream out = new FileOutputStream(entryPath.toFile())) {
                        IOUtils.copy(in, out);
                    }/*from   ww w.j a va  2  s . c  o  m*/
                }
            }
        }
    }
}