Program
import java.io.*; public class FileStringBoolean { public static void main(String[] args) { File f = null; File f1 = null; File f2 = null; File f3 = null; File f4 = null; File f5 = null; File f6 = null; String v; String v2; String v3; String path; boolean bool = false; try { f = new File("c:\\test1.txt"); bool = f.isAbsolute(); path = f.getPath(); System.out.println("1. File isAbsolute() : " + path + " : " + bool); f1 = new File("test2.txt"); f2 = f1.getAbsoluteFile(); bool = f2.exists(); path = f1.getAbsolutePath(); System.out.println("2. File getAbsoluteFile() : " + path + ": " + bool); System.out.println("3. String getAbsolutepath() : " + path); f4 = new File("test3.txt"); f5 = f4.getCanonicalFile(); bool = f5.exists(); path = f4.getAbsolutePath(); System.out.println("4. File getCanonicalFile() : " + path + ":" + bool); System.out.println("5. String getCanonicalpath(): " + path); v = f.getParent(); System.out.println("6. String getParent() : "+ v); f6 = f5.getParentFile(); v2 = f6.getAbsolutePath(); System.out.println("7. File getParentFile() : " + v2); v3 = f5.getPath(); System.out.println("8. String getPath() : " + v2); } catch (Exception e) { e.printStackTrace(); } FileStringBoolean c = new FileStringBoolean(); Class cclass = c.getClass(); String name = cclass.getName(); System.out.println("9. String getName() : " + name); } }
Output
1. File isAbsolute() : c:\test1.txt : true 2. File getAbsoluteFile() : C:\Users\sami\WorkSpace\File\test2.txt: false 3. String getAbsolutepath() : C:\Users\sami\WorkSpace\File\test2.txt 4. File getCanonicalFile() : C:\Users\sami\WorkSpace\File\test3.txt:false 5. String getCanonicalpath(): C:\Users\sami\WorkSpace\File\test3.txt 6. String getParent() : c:\ 7. File getParentFile() : C:\Users\sami\WorkSpace\File 8. String getPath() : C:\Users\sami\WorkSpace\File 9. String getName() : SamiCreation.FileStringBoolean
Description
public boolean isAbsolute()
Tests whether this abstract pathname is absolute.
The definition of absolute pathname is system dependent. On UNIX systems, a pathname is absolute if
its prefix is “/”. On Microsoft Windows systems, a pathname is absolute if its prefix is a drive specifier
followed by “\”, or if its prefix is “\\”.
Returns:
true if this abstract pathname is absolute, false otherwise
public File getAbsoluteFile()
Returns the absolute form of this abstract pathname. Equivalent to new File(this.getAbsolutePath()).
Returns:
The absolute abstract pathname denoting the same file or directory as this abstract pathname
Throws:
SecurityException – If a required system property value cannot be accessed.
public String getAbsolutePath()
Returns the absolute pathname string of this abstract pathname.
If this abstract pathname is already absolute, then the pathname string is simply returned as if by the
getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current
user directory, which is named by the system property user.dir, is returned. Otherwise this pathname is resolved in a
system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory.
On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by
the pathname, if any; if not, it is resolved against the current user directory.
Returns:
The absolute pathname string denoting the same file or directory as this abstract pathname
Throws:
SecurityException – If a required system property value cannot be accessed.
public File getCanonicalFile() throws IOException
Returns the canonical form of this abstract pathname. Equivalent to new File(this.getCanonicalPath()).
Returns:
The canonical pathname string denoting the same file or directory as this abstract pathname
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
public String getCanonicalPath() throws IOException
Returns the canonical pathname string of this abstract pathname.
A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this
pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way.
This typically involves removing redundant names such as “.” and “..” from the pathname, resolving symbolic links (on UNIX platforms), and converting
drive letters to a standard case (on Microsoft Windows platforms).
Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory
also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of
the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be
different from the canonical form of the same pathname after the file or directory is deleted.
Returns:
The canonical pathname string denoting the same file or directory as this abstract pathname
Throws:
IOException – If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
IOException – If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
checkRead(java.io.FileDescriptor) method denies read access to the file
public String getParent()
Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
The parent of an abstract pathname consists of the pathname’s prefix, if any, and each name in the pathname’s name sequence except for the last.
If the name sequence is empty then the pathname does not name a parent directory.
Returns:
The pathname string of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
public File getParentFile()
Returns the abstract pathname of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
The parent of an abstract pathname consists of the pathname’s prefix, if any, and each name in the pathname’s name sequence except for the last.
If the name sequence is empty then the pathname does not name a parent directory.
Returns:
The abstract pathname of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
public String getPath()
Converts this abstract pathname into a pathname string. The resulting string uses the default
name-separator character to separate the names in the name sequence.
Returns:
The string form of this abstract pathname
public String getName()
Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname’s name sequence.
If the pathname’s name sequence is empty, then the empty string is returned.
Returns:
The name of the file or directory denoted by this abstract pathname, or the empty string if this pathname’s name sequence is empty