Java Root Directory Get getRoot(String path)

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

Description

It can be necessary to determine which is the root of a path.

License

Open Source License

Parameter

Parameter Description
path The path used to get a root

Return

The root which contais the specified path

Declaration


public static String getRoot(String path) 

Method Source Code

//package com.java2s;
/*//w  w  w. j  av a2s  .  c o m
 * Copyright (c) 1998 - 2005 Versant Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Versant Corporation - initial API and implementation
 */

import java.io.File;

public class Main {
    /**
     * It can be necessary to determine which is the root of a path.
     * For example, the root of D:\Projects\Java is D:\.
     * @param path The path used to get a root
     * @return The root which contais the specified path
     */

    public static String getRoot(String path) {
        File file = new File(path);
        File[] roots = file.listRoots();
        for (int i = 0; i < roots.length; i++)
            if (path.startsWith(roots[i].getPath()))
                return roots[i].getPath();
        return path;
    }
}

Related

  1. getRoot(File file)
  2. getRoot(File file)
  3. getRoot(File file, ArrayList lst)
  4. getRoot(final File workingDirectory)
  5. getRoot(final String path)
  6. getRootDir()
  7. getRootDir()
  8. getRootDir()
  9. getRootDir()