File toURI() in Java

toURI(): This method is available in the java.io.File class of Java.

Syntax:

URI java.io.File.toURI()

This method constructs a file: URI that represents this abstract pathname.

Note: The exact form of the URI is system-dependent.

Parameters: NA

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.

Approach

Java

import java.io.File;

public class FiletoURI {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        System.out.println(file.toURI());
    }
}

Output:

file:/D:/hello.txt


No comments:

Post a Comment