Example usage for java.nio.file Path getRoot

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

Introduction

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

Prototype

Path getRoot();

Source Link

Document

Returns the root component of this path as a Path object, or null if this path does not have a root component.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {

    Path listing = Paths.get("/usr/bin/zip");

    System.out.println("Root of Path [" + listing.getRoot() + "]");

}

From source file:Main.java

public static void main(String[] args) {

    Path path = Paths.get("C:", "tutorial/Java/JavaFX", "Topic.txt");

    System.out.println(path.getRoot());

}

From source file:Test.java

public static void main(String[] args) {
    Path path = FileSystems.getDefault().getPath("/home/docs/status.txt");

    System.out.printf("getRoot: %s\n", path.getRoot());
}

From source file:Main.java

public static void printDetails(Path p) {
    System.out.println("Details for path: " + p);

    int count = p.getNameCount();
    System.out.println("Name count: " + count);

    for (int i = 0; i < count; i++) {
        Path name = p.getName(i);
        System.out.println("Name at  index   " + i + "  is " + name);
    }/*from w ww  . jav a  2  s  .c  o m*/

    Path parent = p.getParent();
    Path root = p.getRoot();
    Path fileName = p.getFileName();
    System.out.println("Parent: " + parent + ", Root:   " + root + ", File Name: " + fileName);
    System.out.println("Absolute Path: " + p.isAbsolute());
}

From source file:ch.bender.evacuate.Helper.java

/**
 * Deletes a whole directory (recursively)
 * <p>/*from   w  w w  .j  av a  2 s .c o  m*/
 * @param aDir
 *        a folder to be deleted (must not be null)
 * @throws IOException
 */
public static void deleteDirRecursive(Path aDir) throws IOException {
    if (aDir == null) {
        throw new IllegalArgumentException("aDir must not be null");
    }

    if (Files.notExists(aDir)) {
        return;
    }

    if (!Files.isDirectory(aDir)) {
        throw new IllegalArgumentException("given aDir is not a directory");
    }

    Files.walkFileTree(aDir, new SimpleFileVisitor<Path>() {
        /**
         * @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object, java.io.IOException)
         */
        @Override
        public FileVisitResult visitFileFailed(Path aFile, IOException aExc) throws IOException {
            if ("System Volume Information".equals((aFile.getFileName().toString()))) {
                return FileVisitResult.SKIP_SUBTREE;
            }

            throw aExc;
        }

        /**
         * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
         */
        @Override
        public FileVisitResult preVisitDirectory(Path aFile, BasicFileAttributes aAttrs) throws IOException {
            if ("System Volume Information".equals((aFile.getFileName()))) {
                return FileVisitResult.SKIP_SUBTREE;
            }

            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (dir.isAbsolute() && dir.getRoot().equals(dir)) {
                myLog.debug("root cannot be deleted: " + dir.toString());
                return FileVisitResult.CONTINUE;
            }

            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }

    });
}

From source file:de.ncoder.studipsync.storage.StandardPathResolver.java

@Override
public Path resolve(Path root, Download download, Path srcFile) {
    if (srcFile.isAbsolute()) {
        srcFile = srcFile.getRoot().relativize(srcFile);
    }//from  ww  w . ja va2  s.c o m
    Path dstRoot = resolve(root, download).getParent(); //only dir in root of src == dir of download ("Hauptordner")
    Path dstPath = dstRoot.resolve(srcFile.toString());
    log.debug(dstPath + " = " + dstRoot + " <~ " + srcFile);
    return dstPath;
}

From source file:io.undertow.server.handlers.file.FileHandlerSymlinksTestCase.java

@Test
public void testCreateSymlinks() throws IOException, URISyntaxException {
    Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();

    Path newDir = rootPath.resolve("newDir");
    Assert.assertFalse(Files.isSymbolicLink(newDir));

    Path innerDir = newDir.resolve("innerDir");
    Assert.assertFalse(Files.isSymbolicLink(innerDir));

    Path newSymlink = rootPath.resolve("newSymlink");
    Assert.assertTrue(Files.isSymbolicLink(newSymlink));

    Path innerSymlink = newSymlink.resolve("innerSymlink");
    Assert.assertTrue(Files.isSymbolicLink(innerSymlink));

    Path f = innerSymlink.getRoot();
    for (int i = 0; i < innerSymlink.getNameCount(); i++) {
        f = f.resolve(innerSymlink.getName(i).toString());
        System.out.println(f + " " + Files.isSymbolicLink(f));
    }/*from   w ww.j  av a  2 s .  com*/
    f = f.resolve(".");
    System.out.println(f + " " + Files.isSymbolicLink(f));
}

From source file:com.spd.ukraine.lucenewebsearch1.web.IndexingController.java

@PostConstruct
public void init() {
    if (IS_DIRECTORY_IN_DISK) {
        String userDirectory = System.getProperty("user.dir");// + "/lucene"; 
        System.out.println("userDirectory " + userDirectory);
        Path userPath = Paths.get(userDirectory);
        Path rootPath = userPath.getRoot();
        String workingDirectory = rootPath.toString()
                .concat(System.getProperty("file.separator").equals("/")
                        ? userPath.subpath(0, 2).toString() + "/"
                        : "\\Users\\sf\\")
                .concat("luceneindex");
        System.out.println("workingDirectory " + workingDirectory);
        indexDir = new File(workingDirectory);
        try {// w w w. java  2  s. co m
            Files.createDirectory(Paths.get(workingDirectory));
        } catch (FileAlreadyExistsException ex) {
            System.out.println("FileAlreadyExistsException");
        } catch (IOException ex) {
            //            System.out.println("IOException: " + ex.getMessage());
            ex.printStackTrace();
        }
        if (null == indexDir) {
            return;
        }
        try {
            directory = FSDirectory.open(indexDir);
        } catch (IOException ex) {
            System.out.println("IOException: " + ex.getMessage());
        }
    } else {
        directory = new RAMDirectory();
    }
    analyzer = new StandardAnalyzer(Version.LUCENE_43);//new StandardAnalyzer();
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, analyzer);
    try {
        indexWriter = new IndexWriter(directory, config);
    } catch (IOException ex) {
        //            ex.printStackTrace();
        //            return;
    }
}

From source file:org.apache.nifi.toolkit.tls.standalone.TlsToolkitStandaloneCommandLine.java

protected static String calculateDefaultOutputDirectory(Path currentPath) {
    Path currentAbsolutePath = currentPath.toAbsolutePath();
    Path parent = currentAbsolutePath.getParent();
    if (parent == currentAbsolutePath.getRoot()) {
        return parent.toString();
    } else {//w  ww .j a  va  2 s .com
        Path currentNormalizedPath = currentAbsolutePath.normalize();
        return "../" + currentNormalizedPath.getFileName().toString();
    }
}

From source file:org.apache.rya.api.path.PathUtils.java

/**
 * Indicates whether file lives in a secure directory relative to the
 * program's user./*w  w  w.j a va2s.  c  om*/
 * @param file {@link Path} to test.
 * @param user {@link UserPrincipal} to test. If {@code null}, defaults to
 * current user.
 * @param symlinkDepth Number of symbolic links allowed.
 * @return {@code true} if file's directory is secure.
 */
public static boolean isInSecureDir(Path file, UserPrincipal user, final int symlinkDepth) {
    if (!file.isAbsolute()) {
        file = file.toAbsolutePath();
    }
    if (symlinkDepth <= 0) {
        // Too many levels of symbolic links
        return false;
    }
    // Get UserPrincipal for specified user and superuser
    final Path fileRoot = file.getRoot();
    if (fileRoot == null) {
        return false;
    }
    final FileSystem fileSystem = Paths.get(fileRoot.toString()).getFileSystem();
    final UserPrincipalLookupService upls = fileSystem.getUserPrincipalLookupService();
    UserPrincipal root = null;
    try {
        if (SystemUtils.IS_OS_UNIX) {
            root = upls.lookupPrincipalByName("root");
        } else {
            root = upls.lookupPrincipalByName("Administrators");
        }
        if (user == null) {
            user = upls.lookupPrincipalByName(System.getProperty("user.name"));
        }
        if (root == null || user == null) {
            return false;
        }
    } catch (final IOException x) {
        return false;
    }
    // If any parent dirs (from root on down) are not secure, dir is not secure
    for (int i = 1; i <= file.getNameCount(); i++) {
        final Path partialPath = Paths.get(fileRoot.toString(), file.subpath(0, i).toString());
        try {
            if (Files.isSymbolicLink(partialPath)) {
                if (!isInSecureDir(Files.readSymbolicLink(partialPath), user, symlinkDepth - 1)) {
                    // Symbolic link, linked-to dir not secure
                    return false;
                }
            } else {
                final UserPrincipal owner = Files.getOwner(partialPath);
                if (!user.equals(owner) && !root.equals(owner)) {
                    // dir owned by someone else, not secure
                    return SystemUtils.IS_OS_UNIX ? false : Files.isWritable(partialPath);
                }
            }
        } catch (final IOException x) {
            return false;
        }
    }
    return true;
}