Java.io.File.getCanonicalFile() Method
Advertisements
Description
The java.io.File.getCanonicalFile() method returns the canonical form of this abstract pathname.
Declaration
Following is the declaration for java.io.File.getCanonicalFile() method:
public File getCanonicalFile()
Parameters
NA
Return Value
The method returns same file or directory represented by the canonical pathname string
Exception
IOException -- if an I/O error occurs
SecurityException -- if a system property value can not be accessed.
Example
The following example shows the usage of java.io.File.getCanonicalFile() method.
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
File f1 = null;
String path = "";
boolean bool = false;
try{
// create new files
f = new File("C:\\Program Files\\..\\test.txt");
// create new canonical form file object
f1 = f.getCanonicalFile();
// returns true if the file exists
bool = f1.exists();
// returns absolute pathname
path = f1.getAbsolutePath();
// if file exists
if(bool)
{
// prints
System.out.print(path+" Exists? "+ bool);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
C:\test.txt Exists? true