Java Path Is Absolute isAbsolutePath(String filename)

Here you can find the source of isAbsolutePath(String filename)

Description

Returns whether a filename represents an absolute path.

License

Apache License

Declaration

public static boolean isAbsolutePath(String filename) 

Method Source Code

//package com.java2s;
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 The ConQAT Project                                   |
|                                                                          |
| Licensed under the Apache License, Version 2.0 (the "License");          |
| you may not use this file except in compliance with the License.         |
| You may obtain a copy of the License at                                  |
|                                                                          |
|    http://www.apache.org/licenses/LICENSE-2.0                            |
|                                                                          |
| Unless required by applicable law or agreed to in writing, software      |
| distributed under the License is distributed on an "AS IS" BASIS,        |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and      |
| limitations under the License.                                           |
+-------------------------------------------------------------------------*/

public class Main {
    /**//from  w w w  .  j  a va  2 s  .  c o  m
     * Returns whether a filename represents an absolute path.
     * 
     * This method returns the same result, independent on which operating
     * system it gets executed. In contrast, the behavior of
     * {@link File#isAbsolute()} is operating system specific.
     */
    public static boolean isAbsolutePath(String filename) {
        // Unix and MacOS: absolute path starts with slash or user home
        if (filename.startsWith("/") || filename.startsWith("~")) {
            return true;
        }
        // Windows and OS/2: absolute path start with letter and colon
        if (filename.length() > 2 && Character.isLetter(filename.charAt(0)) && filename.charAt(1) == ':') {
            return true;
        }
        // UNC paths (aka network shares): start with double backslash
        if (filename.startsWith("\\\\")) {
            return true;
        }

        return false;
    }
}

Related

  1. isAbsolute(final String path)
  2. isAbsolute(String aPath)
  3. isAbsolute(String path)
  4. isAbsolute(String path)
  5. isAbsolute(String path)
  6. isAbsolutePath(String path)
  7. isAbsolutePath(String path)
  8. isAbsolutePath(String path)
  9. isAbsolutePath(String path)