3:32 pm - Saturday January 21, 2017

This Program displays the absolute path of the file using 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

 

Displays the absolute path of the file. All ‘.’ and ‘..’ references will be resolved.

EXAMPLE PROGRAM


import java.io.*;

public class GetCanonicalPath {
	public static void main(String[] args) {
		try {
			// Open the file C:\MyFile.txt.
			File myFile = new File("C:\\Windows\\..\\MyFile.txt");
			String path = myFile.getCanonicalPath();

			// If the file exists, display it's full path.
			if (myFile.exists()) {
				System.out.println(path + " exists!");
			} else {
				System.out.println(path + " does NOT exist!");
			}
		} catch (IOException ex) {
			System.out.println(ex.toString());
		}
	}
}

Output:

C:\MyFile.txt does NOT exist!

Explanation:

Displays the absolute path of the file. All ‘.’ and ‘..’ references will be resolved.

Parameters:Na

Returnvalue:

The absolute path of the file represented by the File object.

Exception:

  • IOException — if an I/O error occurs
  • SecurityException — if a system property value can not be accessed.

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.