Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { public static String formatToUrlNoPrefix(String filePath) { if (filePath == null) { return null; } filePath = formatToUrl(filePath); if (File.separator.endsWith("/")) { filePath = filePath.substring("file://".length()); } else { filePath = filePath.substring("file:///".length()); } return filePath; } public static String formatToUrl(String filePath) { if (filePath == null) { return null; } filePath = filePath.replaceFirst("file:[/\\\\]*", ""); filePath = filePath.replaceAll("[/\\\\]+", "/"); if (filePath.startsWith("/")) { filePath = "file://" + filePath; } else { filePath = "file:///" + filePath; } return filePath; } }