Java URI from toURI(String path)

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

Description

to URI

License

Open Source License

Declaration

public static URI toURI(String path) 

Method Source Code

//package com.java2s;
import java.net.URI;

public class Main {
    public static URI toURI(String path) {
        return URI.create(treatPath(path));
    }//from w w  w. j a  v  a 2  s.  c  o m

    public static String treatPath(String path) {
        if (path != null && !path.trim().isEmpty()) {

            final String uriSuffix = "file:";
            final String slash = "/";

            path = path.trim();
            path = path.replaceAll("//", slash);
            path = path.replaceAll("\\\\", slash);
            path = path.replaceAll("\\s+", " ");
            path = path.replaceAll("[\\p{Z}]", "%20");

            StringBuilder sb = new StringBuilder();
            if (!path.startsWith(uriSuffix)) {
                if (path.startsWith(slash)) {
                    sb.append(uriSuffix).append(path);
                } else {
                    sb.append(uriSuffix).append(slash).append(path);
                }
            }
            return sb.toString();
        }
        return path;
    }
}

Related

  1. toURI(String location)
  2. toURI(String location)
  3. toURI(String location)
  4. toURI(String location)
  5. toURI(String location)
  6. toUri(String path)
  7. toURI(String s)
  8. toURI(String str)
  9. toURI(String str)