Convert Between a Filename Path and a URL in Java

Description

The following code shows how to convert Between a Filename Path and a URL.

Example


//from w w  w . jav  a  2 s  .c o m
import java.io.File;
import java.io.InputStream;
import java.net.URI;

public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("filename");

    URI uri = file.toURI();
    System.out.println(uri);
    file = new File(uri.toURL().getFile());
    InputStream is = uri.toURL().openStream();
    is.close();
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip