Home » Java programming language

Java File Class URI toURI() method with Example

Java File Class URI toURI() method: Here, we are going to learn about the URI toURI() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 15, 2019

File Class URI toURI()

  • This method is available in package java.io.File.toURI().
  • URI stands for Uniform Resource Identifier.
  • This method creates a file URI (i.e. URI is nothing but it is a filepath).
  • This method will throw a security exception if we access an unauthorized property.
  • This method is accessible with file object.


    URI toURI(){


We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is URI, it returns absolute filepath of the file if exists.

Java program to demonstrate example of toURI() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an exception 
// when working with files
import java.lang.Exception;

// For accessing net files
import java.net.URI;

public class URIClass {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            URI uni_res_iden;
            File file = new File("E:\\Programs\\myjava.txt");

            if (file.exists()) {
                uni_res_iden = file.toURI();
                System.out.println("The URI of the file is : " + " " + uni_res_iden);
        } catch (Exception e) {
            System.out.println("An error occurred.");


E:\Programs>javac URIClass.java

E:\Programs>java URIClass
The URI of the file is :  file:/E:/Programs/myjava.txt

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.