Path toFile() method in Java with Examples
The toFile() method of java.nio.file.Path interface used to return a java.io.File object representing this path object. if this Path is associated with the default provider, then this method returns a java.io.File object constructed with the String representation of this path. If this path was created by invoking the java.io.File toPath method then there is no guarantee that the File object returned by this method is equal to the original File. This method throws UnsupportedOperationException if this Path is not associated with the default provider.
Syntax:
default File toFile()
Parameters: This method accepts nothing.
Return value: This method returns a java.io.File object representing this path.
Exception: This method throws UnsupportedOperationException if this Path is not associated with the default provider.
Below programs illustrate toFile() method:
Program 1:
// Java program to demonstrate // java.nio.file.Path.toFile() method import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) { // create object of Path Path path = Paths.get("D:\\Apps\\" + "NewTextDocument.txt"); // call toFile() to get // File object from path File file = path.toFile(); // print file details System.out.println("File:" + file.toString() + " is readable " + file.canRead()); } } |
Program 2:
// Java program to demonstrate // java.nio.file.Path.toFile() method import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) { // create object of Path Path path = Paths.get("D:\\temp\\" + "AmanSinghCV.docx"); // call toFile() to get // File object from path File file = path.toFile(); // print file details System.out.println("File Name:" + file.getName()); } } |
References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#toFile()
Recommended Posts:
- Path startsWith() method in Java with Examples
- Path endsWith() method in Java with Examples
- Path resolveSibling() method in Java with Examples
- Path iterator() method in Java with Examples
- Path toString() method in Java with Examples
- Path equals() method in Java with Examples
- Path getParent() method in Java with Examples
- Path compareTo() method in Java with Examples
- Path hashCode() method in Java with Examples
- Path toUri() method in Java with Examples
- Path resolve() method in Java with Examples
- Path getRoot() method in Java with Examples
- Path normalize() method in Java with Examples
- Path subpath() method in Java with Examples
- Path relativize() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



