Java file absolute path


 

Java file absolute path

In this section, you will learn how to get the absolute path of the file.

In this section, you will learn how to get the absolute path of the file.

Java file absolute path

In this section, you will learn how to get the absolute path of the file.

Description of code:

If you want to locate a file without requiring further information, you can use absolute path. It always contain the root element and the complete directory list to locate the file. Here we have created an object of File class and passed the name of the file as an argument in the constructor of File class whose absolute path we want to know. Then call the method getAbsolutePath() by the reference of  File class and store it in a String variable which is then displayed on the console.

Here is the code:

import java.io.*;

public class FileAbsolutePath {
	public static void main(String[] args) {
		File file = new File("C:/Answers/File/data.txt");
		String absolutepath = file.getAbsolutePath();
		System.out.println("Absolute Path of the file is: " + absolutepath);
	}
}

Through the getAbsolutePath() method, you can find out the absolute path of any file.

Ads