Tuesday, December 23, 2025
HomeLanguagesJavaJava File Class toURI() Method with Examples

Java File Class toURI() Method with Examples

The toURI() method returns the URI that corresponds to the supplied URL. This method works similarly to the new URI (this.toString()). This abstract pathname is represented by a file: URI.

Syntax:

public URI toURI()

Returns: It returns an absolute hierarchical URI with the scheme “file,” a path that represents this abstract pathname, and undefined authority, query, and fragment components.

Exception:

  • SecurityException: If you can’t get to a needed system property value

Example:

Java




// Java Program to demonstrate the working
// of toURI() method of File Class
  
import java.io.File;
import java.net.URI;
public class GFG 
{
    public static void main (String[] args) 
    {
        File f = new File("C:\\GEEKSFORGEEKS\\hello.txt");
   
        try 
        {
            URI uri = f.toURI();
   
            System.out.println("uri is :- " + uri);
   
        
        catch (SecurityException e) 
        {
            e.printStackTrace();
        }
    }
}


Output:

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6825 POSTS0 COMMENTS
Nicole Veronica
11959 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6912 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS