Java Path Create Canonical canonicalpath(String path)

Here you can find the source of canonicalpath(String path)

Description

Convert path to: 1.

License

Open Source License

Parameter

Parameter Description
path convert this path

Return

canonicalized version

Declaration

static public String canonicalpath(String path) 

Method Source Code

//package com.java2s;
/* Copyright 2012, UCAR/Unidata.
 See the LICENSE file for more information. */

public class Main {
    static final public String DRIVELETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**/*www  .  j  a v a2 s .  com*/
     * Convert path to:
     * 1. use '/' consistently
     * 2. remove any trailing '/'
     * 3. trim blanks
     *
     * @param path convert this path
     * @return canonicalized version
     */
    static public String canonicalpath(String path) {
        if (path == null)
            return null;
        path = path.trim();
        path = path.replace('\\', '/');
        if (path.endsWith("/"))
            path = path.substring(0, path.length() - 1);
        // As a last step, lowercase the drive letter, if any
        if (hasDriveLetter(path))
            path = path.substring(0, 1).toLowerCase() + path.substring(1);
        return path;
    }

    /**
     * return true if this path appears to start with a windows drive letter
     *
     * @param path
     * @return
     */
    static public boolean hasDriveLetter(String path) {
        if (path != null && path.length() >= 2) {
            return (DRIVELETTERS.indexOf(path.charAt(0)) >= 0 && path
                    .charAt(1) == ':');
        }
        return false;
    }
}

Related

  1. canonicalPath(String orig)
  2. canonicalPath(String path)
  3. canonicalpath(String path, boolean relative)