Java Path Normalize normalizePath(String path)

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

Description

normalize / to \ and remove trailing slashes from a path

License

Open Source License

Parameter

Parameter Description
path a parameter

Exception

Parameter Description
Exception an exception

Return

normalized path

Declaration

public static String normalizePath(String path) throws Exception 

Method Source Code

//package com.java2s;
/*************************************************************************************** 
 * Copyright (C) 2007 Samatar.  All rights reserved. 
 * This software was developed by Samatar and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. A copy of the license, 
 * is included with the binaries and source code. The Original Code is Samatar.  
 * The Initial Developer is Samatar.//from ww  w.  j a va 2 s. c  o m
 *
 * Software distributed under the GNU Lesser Public License is distributed on an 
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
 * Please refer to the license for the specific language governing your rights 
 * and limitations.
 ***************************************************************************************/

public class Main {
    public static String FILE_SEPARATOR = "/";

    /**
    * normalize / to \ and remove trailing slashes from a path
    * 
    * @param path
    * @return normalized path
    * @throws Exception
    */
    public static String normalizePath(String path) throws Exception {
        if (path == null)
            return path;
        String normalizedPath = path.replaceAll("\\\\", FILE_SEPARATOR);
        while (normalizedPath.endsWith("\\") || normalizedPath.endsWith(FILE_SEPARATOR)) {
            normalizedPath = normalizedPath.substring(0, normalizedPath.length() - 1);
        }

        return normalizedPath;
    }
}

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)