Java Canonical Path Create getCanonicalPath(String path)

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

Description

Canonicalize the specified file path according to the current platform's rules:
  • Condense multiple consecutive path separators into a single path separator.
  • Remove "."

    License

    Open Source License

    Parameter

    Parameter Description
    path the file path to be canonicalized

    Return

    the canonicalized file path

    Declaration

    public static String getCanonicalPath(String path) 
    

    Method Source Code

    //package com.java2s;
    /*//from  w w  w.ja va  2s  .  c  o  m
     * RHQ Management Platform
     * Copyright (C) 2005-2008 Red Hat, Inc.
     * All rights reserved.
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License, version 2, as
     * published by the Free Software Foundation, and/or the GNU Lesser
     * General Public License, version 2.1, also as published by the Free
     * Software Foundation.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     * GNU General Public License and the GNU Lesser General Public License
     * for more details.
     *
     * You should have received a copy of the GNU General Public License
     * and the GNU Lesser General Public License along with this program;
     * if not, write to the Free Software Foundation, Inc.,
     * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
     */
    
    import java.io.File;
    
    import java.io.IOException;
    
    public class Main {
        /**
         * Canonicalize the specified file path according to the current platform's rules:
         * <ul>
         *   <li>Condense multiple consecutive path separators into a single path separator.</li>
         *   <li>Remove "." path elements.</li>
         *   <li>Resolve ".." path elements.</li>
         *   <li>On Windows, normalize capitalization.</li>
         *   <li>On Windows, expand 8.3-abbreviated path elements (e.g. "DOCUME~1" -> "Documents and Settings").</li>
         * </ul>
         * <p/>
         * Unlike {@link File#getCanonicalPath()}, this method does <b>not</b> resolve symlinks.</li>
         * <p/>
         * The path may or may not reference an existing file.
         *
         * @param path the file path to be canonicalized
         *
         * @return the canonicalized file path
         */
        public static String getCanonicalPath(String path) {
            File file = new File(path);
            if (isUnix()) {
                // UNIX - Do *not* use File#getCanonicalFile, since it will resolve symlinks.
                file = new File(file.toURI().normalize()).getAbsoluteFile();
            } else {
                // Windows - Use File#getCanonicalFile(), since it will normalize
                // capitalization and will not resolve junctions (i.e. the NTFS
                // equivalent of symlinks).
                try {
                    file = file.getCanonicalFile();
                } catch (IOException e) {
                    // best we can do...
                    file = new File(file.toURI().normalize()).getAbsoluteFile();
                }
            }
            return file.getPath();
        }
    
        private static boolean isUnix() {
            return File.separatorChar == '/';
        }
    }
    

    Related

    1. getCanonicalPath(String name)
    2. getCanonicalPath(String outputPath)
    3. getCanonicalPath(String path)
    4. getCanonicalPath(String path)
    5. getCanonicalPath(String path)
    6. getCanonicalPath(String path)
    7. getCanonicalPath(String path)
    8. getCanonicalPath(String path)
    9. getCanonicalPath(String path)