Java.io.File.getPath() Method


Advertisements


Description

The java.io.File.getPath() method converts the abstract pathname into pathname string. To separate the names in the name sequence the resulting string uses the default name-separator character.

Declaration

Following is the declaration for java.io.File.getPath() method −

public String getPath()

Parameters

NA

Return Value

The method returns pathname string form of this abstract pathname.

Exception

NA

Example

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

package com.tutorialspoint;

import java.io.File;

public class FileDemo {

   public static void main(String[] args) {      
      File f = null;
      String v;
      boolean bool = false;
      
      try {
      
         // create new file
         f = new File("test.txt");
         
         // pathname string from abstract pathname
         v = f.getPath();
         
         // true if the file path exists
         bool = f.exists();
         
         // if file exists
         if(bool) {
         
            // prints
            System.out.print("pathname: "+v);
         }
         
      } catch(Exception e) {
         
         // if any error occurs
         e.printStackTrace();
      }
   }
}

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

pathname: test.txt

java_io_file.htm

Advertisements