java files

java.net.URI() uri in java Code With Examples

Table of Contents

Program

import java.io.File;
import java.net.URI;
public class FileUri 
{
	public static void main(String[] args) 
       {
		File directory = new File("helloworld");
		try 	
	       {
			URI uri = directory.toURI();
			System.out.println("uri:- " + uri);
		} 
                catch (SecurityException e) 
                {
			e.printStackTrace();
		}
	}
}

Output

uri:- file:/C:/Users/Venkatesh/Desktop/helloworld

Description

public URI toURI()

Constructs a file: URI that represents this abstract pathname.
The exact form of the URI is system-dependent. If it can be determined that the file denoted by this abstract pathname is a directory, then the resulting URI will end with a slash.

For a given abstract pathname f, it is guaranteed that

new File( f.toURI()).equals( f.getAbsoluteFile())

so long as the original abstract pathname, the URI, and the new abstract pathname are all created in (possibly different invocations of) the same Java virtual machine. Due to the system-dependent nature of abstract pathnames, however, this relationship typically does not hold when a file: URI that is created in a virtual machine on one operating system is converted into an abstract pathname in a virtual machine on a different operating system.

Note that when this abstract pathname represents a UNC pathname then all components of the UNC (including the server name component) are encoded in the URI path. The authority component is undefined, meaning that it is represented as null. The Path class defines the toUri method to encode the server name in the authority component of the resulting URI. The toPath method may be used to obtain a Path representing this abstract pathname.

Returns:

An absolute, hierarchical URI with a scheme equal to “file”, a path representing this abstract pathname, and undefined authority, query, and fragment components

Throws:

SecurityException – If a required system property value cannot be accessed.