Convert file path to URL
In this chapter you will learn:
Convert file path to URL
The following code calls the file.URL().toURL() to convert a file path to a URL.
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
/*from j a v a 2s . c o m*/
public class Main {
public static void main(String[] argv)throws MalformedURLException {
URL url = convertFileToURL( "c:/abc/def.htm");
System.out.println(url);
}
public static URL convertFileToURL( String filePath ) throws MalformedURLException {
File file = new File(filePath.trim());
return file.toURI().toURL();
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » URL URI