File toPath() in Java

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

Syntax:

Path java.io.File.toPath()

This method returns a java.nio.file.Path object constructed from this abstract path.

Note: The resulting Path is associated with the default file system.

Parameters: NA

Returns: a Path constructed from this abstract path.

Throws:

java.nio.file.InvalidPathException - if a Path object cannot be constructed from the abstract path.

Approach

Java

import java.io.File;

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

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

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

Output:

D:\hello.txt


No comments:

Post a Comment