Java Path Is Absolute isAbsolutePath(String path)

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

Description

Checks if the specified path is absolute.

License

LGPL

Parameter

Parameter Description
The path to check.

Return

true if the path is absolute.

Declaration

public static boolean isAbsolutePath(String path) 

Method Source Code

//package com.java2s;
/*//  w w w .j  a v a 2  s.c o m
 * JLib - Publicitas Java library v1.2.0.
 *
 * Copyright (c) 2005, 2006, 2007, 2008, 2009 Publicitas SA.
 * Licensed under LGPL (LGPL-LICENSE.txt) license.
 */

public class Main {
    /**
     * Checks if the specified path is absolute.
     * <ul>
     *    <li><b>In Windows</b> the path is absolute if it starts with a letter unit:
     *    <pre>C:/this/that/...</pre></li>
     *    <li><b>In Unix</b> the path is absolute if it starts with a "/":
     *    <pre>/root/data/...</pre></li>
     * </ul>
     * @param The path to check.
     * @return <i>true</i> if the path is absolute.
     */
    public static boolean isAbsolutePath(String path) {
        String osName;
        if (path == null)
            return false;
        if (path.length() == 0)
            return false;
        osName = System.getProperty("os.name").toLowerCase();

        if (osName.startsWith("win")) {
            if (path.length() < 2)
                return false;
            if (path.charAt(1) == ':')
                return true;
        } else if (path.startsWith("/"))
            return true;
        return false;
    }
}

Related

  1. isAbsolutePath(String path)
  2. isAbsolutePath(String path)
  3. isAbsolutePath(String path)
  4. isAbsolutePath(String path)
  5. isAbsolutePath(String path)
  6. IsAbsolutePath(String strPath)