Java Path Remove nio removeDriveLetter(Path path)

Here you can find the source of removeDriveLetter(Path path)

Description

Remove the root component from the path, returning a relative path.

License

Open Source License

Parameter

Parameter Description
path path to remove root from

Return

a relative path

Declaration

static public Path removeDriveLetter(Path path) 

Method Source Code

//package com.java2s;
/* The MIT License (MIT)
 * Copyright (c) 2014 Nicholas Wright/*from ww w  . ja v a 2 s . co m*/
 * http://opensource.org/licenses/MIT
 */

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**
     * Remove the root component from the path, returning a relative path. If the path is already relative, it will not be changed. C:\temp\
     * becomes \temp\
     * 
     * @param path
     *            path to remove root from
     * @return a relative path
     */
    static public Path removeDriveLetter(Path path) {
        if (path == null) {
            return null;
        }

        if (path.isAbsolute()) {
            return Paths.get(path.getRoot().relativize(path).toString());
        } else {
            return path;
        }
    }

    static public String removeDriveLetter(String path) {
        if (path == null) {
            return null;
        }

        Path relPath = removeDriveLetter(Paths.get(path));
        String filename = relPath.getFileName().toString();

        if (filename.contains(".")) {
            return relPath.toString();
        } else {
            return relPath.toString() + "\\";
        }
    }
}

Related

  1. recursiveRemoveFolder(Path folderPath)
  2. removeDirectory(Path directory)
  3. removeDirectory(Path directory)
  4. removeDirectory(String pathToDir)
  5. removeDirectoryIfItIsEmpty(Path directoryToRemove)
  6. removeFile(final String removePath)
  7. removeFile(String path)
  8. removeFile(String workspacePath)
  9. removeLine(String path, String line, boolean commentAware)