Here you can find the source of normalizePath(String path)
private static String normalizePath(String path)
//package com.java2s; public class Main { private static String normalizePath(String path) { int len = path.length(); if (len == 0 || path.equals("..") || path.equals(".")) { return ""; }// www .j a v a 2 s .c om if (path.indexOf("/. ") < 0 && path.indexOf("./ ") < 0) { return path; } StringBuilder builder = new StringBuilder(); int index = 0; while (index < len) { char c = path.charAt(index); if ((index + 3 <= len && c == '/' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '/') || (index + 2 == len && c == '.' && path.charAt(index + 1) == '.')) { // begins with "/./" or is ".."; // move index by 2 index += 2; continue; } if (index + 3 <= len && c == '.' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '/') { // begins with "../"; // move index by 3 index += 3; continue; } if ((index + 2 <= len && c == '.' && path.charAt(index + 1) == '/') || (index + 1 == len && c == '.')) { // begins with "./" or is "."; // move index by 1 ++index; continue; } if (index + 2 == len && c == '/' && path.charAt(index + 1) == '.') { // is "/."; append '/' and break builder.append('/'); break; } if (index + 3 == len && c == '/' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '.') { // is "/.."; remove last segment, // append "/" and return int index2 = builder.length() - 1; while (index2 >= 0) { if (builder.charAt(index2) == '/') { break; } --index2; } if (index2 < 0) { index2 = 0; } builder.setLength(index2); builder.append('/'); break; } if (index + 4 <= len && c == '/' && path.charAt(index + 1) == '.' && path.charAt(index + 2) == '.' && path.charAt(index + 3) == '/') { // begins with "/../"; remove last segment int index2 = builder.length() - 1; while (index2 >= 0) { if (builder.charAt(index2) == '/') { break; } --index2; } if (index2 < 0) { index2 = 0; } builder.setLength(index2); index += 3; continue; } builder.append(c); ++index; while (index < len) { // Move the rest of the // path segment until the next '/' c = path.charAt(index); if (c == '/') { break; } builder.append(c); ++index; } } return builder.toString(); } }