Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Java File Class toString() Method with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:

--> Package
    --> File Class
        --> toString() Method   


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.



// Java Program to Illustrate toString() Method
// of File Class
// Importing required classes
// Class
public class GFG {
    // Main driver method
    public static void main(String args[])
        // Try block to check for exceptions
        try {
            // Creating a file
            File file = new File("GeeksForGeeks.txt");
            File txt = new File("./GeeksForGeeks.txt");
        // Catch block to handle the exceptions
        catch (Exception e) {
            // Display exception with line number
            // using printStackTrace() method

Output: Generated on Powershell/ Terminal

My Personal Notes arrow_drop_up
Last Updated : 08 Mar, 2022
Like Article
Save Article
Similar Reads
Related Tutorials