Java Path Normalize normalizePath(String path)

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

Description

normalize Path

License

Open Source License

Declaration

public static String normalizePath(String path) 

Method Source Code

//package com.java2s;
/* 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 (at your option) 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.
 * //  www.j av  a2s. c o m
 * 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.
 *
 * monoped@users.sourceforge.net
 */

public class Main {
    public static String normalizePath(String path) {
        String[] comps = path.split("/");
        String[] ap = new String[comps.length];
        int j = 0;
        boolean absolute = comps[0].length() == 0;

        for (int i = 0; i < comps.length; ++i) {
            String s = comps[i];

            if (s.equals(".") || s.length() == 0)
                continue;

            if (s.equals("..")) {
                if (j > 0)
                    --j;
            } else
                ap[j++] = s;
        }

        if (j == 0)
            return absolute ? "/" : ".";

        if (absolute)
            path = "/" + ap[0];
        else
            path = ap[0];

        for (int i = 1; i < j; ++i)
            path += "/" + ap[i];

        return path;
    }
}

Related

  1. normalizePath(String path)
  2. normalizePath(String path)
  3. normalizePath(String path)
  4. normalizePath(String path)
  5. normalizePath(String path)
  6. normalizePath(String path)
  7. normalizePath(String path)
  8. normalizePath(String path)
  9. normalizePath(String path)