Get an Absolute Filename Path from a Relative Filename with Path in Java


The method java.io.File.getAbsoluteFile() can be used to acquire the absolute filename path from a relative filename with path in Java. This method requires no parameters. It returns the file that is defined by the path name.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.io.File;
public class Demo {
   public static void main(String[] argv) throws Exception {
      File file = new File("c:" + File.separator + "JavaProgram" + File.separator + "demo1.txt");
      file = file.getAbsoluteFile();
      System.out.println(file);
   }
}

The output of the above program is as follows −

Output

c:\JavaProgram\demo1.txt

Now let us understand the above program.

The method java.io.File.getAbsoluteFile() is used to acquire the absolute filename path from a relative filename with path. Then this is printed. A code snippet that demonstrates this is given as follows −

File file = new File("c:" + File.separator + "JavaProgram" + File.separator + "demo1.txt");
file = file.getAbsoluteFile();
System.out.println(file);

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements