4:30 pm - Saturday September 23, 2017

This Program How to create a new File object by calling getAbsoluteFile() 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

It appears that new File(this.getAbsolutePath()) should not be interpreted as being equivalent toinvoking getAbsoluteFile().

EXAMPLE PROGRAM

import java.io.*;

public class Program {
	public static void main(String[] args) {
		// Open the file C:\MyFile.txt.
		File myFile = new File("C:\\MyFile.txt");
		File myFile2 = myFile.getAbsoluteFile();
		String path = myFile2.getAbsolutePath();

		// If the file exists, display it's full path.
		if (myFile2.exists()) {
			System.out.println(path + " exists!");
		} else {
			System.out.println(path + " does not exist!");
		}
	}
}

Output:

              C:\MyFile.txt does not exist!

Explanation:

0down voteaccepted It appears that new File(this.getAbsolutePath()) should not be interpreted as being equivalent toinvoking getAbsoluteFile(). Rather the documentation means that the implementation of getAbsoluteFile() is equivalent to return new File(this.getAbsolutePath()).

Parameters:Na

ReturnValue:

                    A new instance of a File object representing the file located at the absolute path of the current File object.

Exception:
  • SecurityException — if a system property value can not be accessed.

 

 

 

Filed in: Z-A All Java Codes

Comments are closed.