6:15 am - Saturday May 27, 2017

This Program returns the canonical form of this abstract pathname using getCanonicalFile() 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.getCanonicalFile() method returns the canonical form of this abstract pathname.

EXAMPLE PROGRAM


import java.io.*;

public class CanonicalFile {
	public static void main(String[] args) {
		try {
			// Open the file C:\MyFile.txt.
			File myFile = new File("C:\\MyFile.txt");
			File myFile2 = myFile.getCanonicalFile();
			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!");
			}
		} catch (IOException ex) {
			System.out.println(ex.toString());
		}
	}
}

Output:

C:\MyFile.txt does not exist!

Explanation:

The java.io.File.getCanonicalFile() method returns the canonical form of this abstract pathname.

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

Parameters:Na

Returnvalue:

A new instance of a File object representing the file located at the absolute path of the current 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.