Home Tutorial Java Core Files Java file absolute path


Share on Google+Share on Google+
Java file absolute path
Posted on: April 14, 2006 at 12:00 AM
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.


Related Tags for Java file absolute path:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2006

Recommend the tutorial

Advertisements Advertisements