Java URL Create createURL(String fileName)

Here you can find the source of createURL(String fileName)

Description

create URL

License

Open Source License

Declaration

public static URL createURL(String fileName) throws Exception 

Method Source Code


//package com.java2s;

import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static URL createURL(String fileName) throws Exception {
        try {//www .  ja  va  2 s .c  o  m
            return new URL(fileName);
        } catch (MalformedURLException ex) {
            File file = new File(fileName);

            String path = file.getAbsolutePath();

            String fs = System.getProperty("file.separator");

            if (fs.length() == 1) {
                char sep = fs.charAt(0);

                if (sep != '/') {
                    path = path.replace(sep, '/');
                }

                if (path.charAt(0) != '/') {
                    path = '/' + path;
                }
            }

            path = "file://" + path;

            return new URL(path);
        }
    }
}

Related

  1. convertToURLs(String[] hosts)
  2. createURL(final String address)
  3. createURL(final String protocol, final String userInfo, final String host, final int port, final String path, final String query, final String ref)
  4. createUrl(final String spec)
  5. createURL(final String url)
  6. createUrl(String spec)
  7. createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
  8. createURL(String src, File basedir)
  9. createURL(String str)