Java.io.File.getAbsolutePath() Method


Advertisements


Description

The java.io.File.getAbsolutePath() method returns the absolute pathname string of this abstract pathname.

Declaration

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

public String getAbsolutePath()

Parameters

NA

Return Value

The method returns the absolute pathname denoting the same file or directory.

Exception

SecurityException − If a system property value can not be accessed.

Example

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

package com.tutorialspoint;

import java.io.File;

public class FileDemo {

   public static void main(String[] args) {      
      File f = null;
      String path = "";
      boolean bool = false;
      
      try {
      
         // create new files
         f = new File("test.txt");
         
         // returns true if the file exists
         bool = f.exists();
         
         // if file exists
         if(bool) {
         
            // get absolute path
            path = f.getAbsolutePath();
            
            // prints
            System.out.print("Absolute Pathname "+ path);
         }
         
      } catch(Exception e) {
      
         // if any error occurs
         e.printStackTrace();
      }
   }
}

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

Absolute Path D:\EclipseAndroid\IO\BufferedInputStream\test.txt

java_io_file.htm

Advertisements