java.io.File getCanonicalFile()
Description
Throws:
- IOException – If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries.
- SecurityException – If a required system property value cannot be accessed, or if a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the file.
Method Syntax
public File getCanonicalFile()
throws IOException
Method Argument
Data Type | Parameter | Description |
---|---|---|
N/A | N/A | N/A |
Method Returns
This method returns a File which denotes the canonical pathname string denoting the same file or directory as this abstract pathname.
Compatibility
Requires Java 1.2 and up
Java File getCanonicalFile() Example
Below is a java code demonstrates the use of getCanonicalFile() method of File class. The example presented might be simple however it shows the behaviour of the getCanonicalFile() method of File class. Basically we have put a check first if the file exists or not. If the file exists based on the return value of the exists() method, we print the result of the getCanonicalFile() method to illustrate it’s behaviour.
package com.javatutorialhq.java.examples; import java.io.File; import java.io.IOException; /* * This example source code demonstrates the use of * getCanonicalFile() method of File class. * */ public class FileGetCanonicalFileExample { public static void main(String[] args) { // initialize File object File file = new File("C:javatutorialhqinputtest_file.txt"); boolean result; // check if file exists result = file.exists(); try { if (result) { // print message that file exists System.out.println(file.getCanonicalFile() + " exists"); } else { // print message that the file does not exist System.out.println(file.getCanonicalFile() + " does not exists"); } } catch (IOException e) { e.printStackTrace(); } } }