Path toUri() method in Java with Examples

The toUri() method of java.nio.file.Path interface used to return a URI to represent this path. This method converts this path into an absolute URI with a scheme equal to the URI scheme that identifies the provider. The exact form of the scheme-specific part is highly provider dependent. In the scenario of the default provider, the URI is hierarchical with a path component that is absolute. The query and fragment components are undefined. Whether the authority component is defined or not is implementation-dependent. There is no guarantee that the URI may be used to construct a java.io.File. In particular, if this path represents a Universal Naming Convention (UNC) path, then the UNC server name may be encoded in the authority component of the resulting URI. In the case of the default provider, and the file exists, and it can be determined that the file is a directory, then the resulting URI will end with a slash.

Syntax:

URI toUri()

Parameters: This method accepts nothing.



Return value: This method returns the URI representing this path.

Exception: This method throws following Exceptions:

  • IOError– if an I/O error occurs obtaining the absolute path, or where a file system is constructed to access the contents of a file as a file system, and the URI of the enclosing file system cannot be obtained
  • SecurityException– In the case of the default provider, and a security manager is installed, the toAbsolutePath method throws a security exception.

Below programs illustrate toUri() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// java.nio.file.Path.toUri() method
  
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
    public static void main(String[] args)
    {
  
        // create an object of Path
        Path path
            = Paths.get("\\temp\\Spring");
  
        // call toUri() to convert
        // path in URI
        URI uri = path.toUri();
  
        // print URI
        System.out.println("URI: "
                           + uri);
    }
}

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// java.nio.file.Path.toUri() method
  
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
    public static void main(String[] args)
    {
  
        // create an object of Path
        Path path
            = Paths.get("D:\\eclipse\\configuration"
                        + "\\org.eclipse.update\\history");
  
        // call toUri() to convert
        // path in URI
        URI uri = path.toUri();
  
        // print URI
        System.out.println("URI: "
                           + uri);
    }
}

chevron_right


Output:

Reference: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#toUri()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.