Java File Class toString() Method with Examples
Last Updated :
08 Mar, 2022
This method returns the pathname string of the File object is returned by the function toString() function. This method is the same as calling the Java IO getPath() method.
Package view is as follows:
--> java.io Package
--> File Class
--> toString() Method
Syntax:
public String toString()
Return Type: A string form of this abstract pathname.
Exceptions Thrown: SecurityException is thrown when we don’t have access to a file or directory.
Implementation: Prints the results of the function toString() function on numerous File objects in this example code.
- Create file instance new File(“GeeksForGeeks.txt”);
- file.toString(); It will convert the specified pathname into the string.
Example:
Java
import java.io.File;
public class GFG {
public static void main(String args[])
{
try {
File file = new File( "GeeksForGeeks.txt" );
File txt = new File( "./GeeksForGeeks.txt" );
System.out.println(file.toString());
System.out.println(txt.toString());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output: Generated on Powershell/ Terminal
Share your thoughts in the comments
Please Login to comment...