3:09 pm - Thursday October 21, 0720

This Program will give you the full complete path name (filepath + filename) of a file using File.getAbsolutePath() method.

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 

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

EXAMPLE PROGRAM


import java.io.File;
import java.io.IOException;

public class AbsoluteFilePath {
	public static void main(String[] args) {
		try {
			File temp = File.createTempFile("i-am-a-temp-file", ".tmp");
			String absolutePath = temp.getAbsolutePath();
			System.out.println("File path : " + absolutePath);
			String filePath = absolutePath.substring(0,
					absolutePath.lastIndexOf(File.separator));
			System.out.println("File path : " + filePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output:

File path : C:\Users\welcome\AppData\Local\Temp\i-am-a-temp-file1516.tmp

File path : C:\Users\welcome\AppData\Local\Temp

Explanation:

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

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

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.

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.