Java.io.File.toString() Method

Advertisements


Description

The java.io.File.toString() method returns the pathname string returned by the getPath() method of this abstract pathname.

Declaration

Following is the declaration for java.io.File.toString() method:

public String toString()

Parameters

  • NA

Return Value

The method returns string form of this abstract pathname.

Exception

  • NA

Example

The following example shows the usage of java.io.File.toString() method.

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {
      
      File f = null;
      String str = "";
      boolean bool = false;
      
      try{      
         // create new File object
         f = new File("test.txt");
         
         // returns true if file exists
         bool = f.exists();
         
         // if file exists
         if(bool)
         {
            // pathname string of this abstract pathname
            str = f.toString();
            
            // print
            System.out.println("pathname string: "+str);
         }      
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

pathname string: test.txt



Advertisements
Advertisements