java files

Java File toPath() Code With Examples

Table of Contents

Program

import java.io.File;
public class FilePath
{
	public static void main(String[] args)
	{
	    File file = new File("venkatesh.txt");
	    System.out.println("File Path is :- " + file.toPath());
	}
}

Output

File Path is :- venkatesh.txt

Description

public Path toPath()

Returns a java.nio.file.Path object constructed from this abstract path. The resulting Path is associated with the default-filesystem.
The first invocation of this method works as if invoking it were equivalent to evaluating the expression:

FileSystems.getDefault().getPath(this.getPath());

Subsequent invocations of this method return the same Path.
If this abstract pathname is the empty abstract pathname then this method returns a Path that may be used to access the current user directory.

Returns:

a Path constructed from this abstract path

Throws:

InvalidPathException – if a Path object cannot be constructed from the abstract path