Java I/O How to - Get URI from File








Question

We would like to know how to get URI from File.

Answer

/*from w w w  .  j a va 2s  .  com*/
import java.io.File;

public class Main {
  public static void main(String args[]) {
    try {
      File testFile = new File("C:\\books\\2004\\test.txt");
      System.out.println(testFile.toURI().toString());
    } catch (Exception exception) {
      System.out.println("exception");
    }
  }
}

The code above generates the following result.