Determines whether a path is absolute or relative and translates it to a URL accordingly. - Java java.net

Java examples for java.net:URL Convert

Description

Determines whether a path is absolute or relative and translates it to a URL accordingly.

Demo Code


//package com.java2s;

import java.net.*;

public class Main {
    /**//from  www. j a va 2  s .  co  m
     * Determines whether a path is absolute or relative and translates it to a URL accordingly.
     */
    public static URL translateURL(String path) {
        URL result = null;
        try {
            //Still needs a bit of tweaking to make it work on Windows
            if ("/".equals(path.substring(0, 1))) {
                result = new URL("file:" + path);
            } else {
                //This works in or out of jar files
                result = Thread.currentThread().getContextClassLoader()
                        .getResource(path);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}

Related Tutorials