Skip to content
Related Articles

Related Articles

Improve Article
Path toUri() method in Java with Examples
  • Last Updated : 18 Jul, 2019

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:




// 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);
    }
}
Output:

Program 2:




// 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);
    }
}
Output:

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :