Java Root Directory Get getRoot(final String path)

Here you can find the source of getRoot(final 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(final String path) 

Method Source Code


//package com.java2s;
/*/*from   w w  w.j av  a2  s .com*/
 * 20:25:20 20/05/99
 *
 * The Java Shell: Utilities.
 * (C)1999 Romain Guy, Osvaldo Pinali Doederlein.
 *
 * LICENSE
 * =======
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CHANGES
 * =======
 * 1.0.8 - Filled the listRoots method                   (Romain & Osvaldo)
 * 1.0.7 - Several bug fixes in constructPath            (Romain)
 * 1.0.6 - Split JDK1.1/1.2                              (Osvaldo)
 * 1.0.5 - Important bug fix in constructPath(String)    (Romain)
 * 1.0.4 - Added getSize(Enumeration)                    (Osvaldo)
 * 1.0.3 - Changed sortStrings bubble-sort algorithm to  (Romain)
 *         quick-sort (James Gosling)
 * 1.0.2 - Fixed two little bug in constructPath(String) (Romain)
 * 1.0.1 - Added listFiles(String[], boolean)            (Romain)
 *       - Removed unecessary createWhiteSpace(int)      (Romain)
 *       - Modified getWildCardMatches(String, boolean)  (Romain)
 *       - Slighty improved javadoc comments             (Romain)
 * 1.0.0 - Initial release.                              (Romain & Osvaldo)
 *
 * LINKS
 * =====
 * Contact: mailto@osvaldo.visionnaire.com.br
 * Site #1: http://www.geocities.com/ResearchTriangle/Node/2005/
 * Site #2: http://student.vub.ac.be/~opinalid/
 */

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(final String path) {
        final File roots[] = listRoots(new File(path));

        for (int i = 0; i < roots.length; i++) {
            if (path.startsWith(roots[i].getPath())) {
                return roots[i].getPath();
            }
        }

        return path;
    }

    /**
     * We override a Java2 specific method.
     *
     * @param f
     *                A File
     * @return A list of standards roots
     */

    public static File[] listRoots(final File f) {
        if (System.getProperty("os.name").startsWith("Windows")) {
            return new File[] { new File("A:\\"), new File("B:\\"), new File("C:\\"), new File("D:\\"),
                    new File("E:\\"), new File("F:\\"), new File("G:\\"), new File("H:\\"), new File("I:\\") };
            //return new File[]{new File(System.getProperty("java.home").substring(0, 3))};
        } else {
            return new File[] { new File("/") };
        }
    }
}

Related

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