Java.io.File.toURI() Method
Advertisements
Description
The java.io.File.toURI() method creates a file: URI that represents the abstract pathname.
Declaration
Following is the declaration for java.io.File.toURI() method:
public URI toURI()
Parameters
NA
Return Value
The method returns an absolute, hierarchial URI with a scheme equal to "file".
Exception
SecurityException -- If a required system property value cannot be accessed
Example
The following example shows the usage of java.io.File.toURI() method.
package com.tutorialspoint;
import java.io.File;
import java.net.URI;
public class FileDemo {
public static void main(String[] args) {
File f = null;
URI uri;
boolean bool = false;
try{
// create new File object
f = new File("c:/java test.txt");
// returns true if file exists
bool = f.exists();
// if file exists
if(bool)
{
// returns the uri string
uri = f.toURI();
// print
System.out.println("uri: "+uri);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
uri: file:/c:/java%20test.txt