Here you can find the source of normalizePath(String filepath)
Parameter | Description |
---|---|
filepath | The file path to normalize. |
public static String normalizePath(String filepath)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w ww . jav a2 s .co m*/ * Normalizes a file path by replacing various special * path tokens (., .., etc.) with their canonical equivalents. * @param filepath The file path to normalize. * @return The normalized file path. */ public static String normalizePath(String filepath) { if (filepath.startsWith("./") || filepath.startsWith(".\\")) { filepath = filepath.substring(2); } filepath = filepath.replace("/./", "/"); filepath = filepath.replace("\\.\\", "\\"); int endPos = filepath.indexOf("/../"); while (endPos != -1) { int startPos = endPos - 1; while (startPos >= 0 && '/' != filepath.charAt(startPos)) { startPos--; } if (startPos > 0) { filepath = filepath.substring(0, startPos) + "/" + filepath.substring(endPos + 4); } else { filepath = filepath.substring(endPos + 4); } endPos = filepath.indexOf("/../"); } endPos = filepath.indexOf("\\..\\"); while (endPos != -1) { int startPos = endPos - 1; while (startPos >= 0 && '\\' != filepath.charAt(startPos)) { startPos--; } if (startPos > 0) { filepath = filepath.substring(0, startPos) + "\\" + filepath.substring(endPos + 4); } else { filepath = filepath.substring(endPos + 4); } endPos = filepath.indexOf("\\..\\"); } return filepath; } }