A path is separated into directory and file parts by a char stored in the static field separatorChar and
available as a String in the static field separator. The last occurrence of this character in the path
separates the pathname into directory and file components. (Directory is the term used on most systems; some
systems call such an entity a "folder" instead.)
File objects are created with one of four constructors:
publicFile(String path)
Creates a File object to manipulate the specified path.
publicFile(String dirName, String name)
Creates a File object for the file name in the directory named dirName. If
dirName is null, only name is used. If dirName is an empty string,
name is resolved against a system dependent default directory. Otherwise,
this is equivalent to using File(dirName + File.separator +
name).
publicFile(File fileDir, String name)
Creates a File object for the file name in the directory named by the File
object fileDir. Equivalent to using File(fileDir.getPath(),
name).
publicFile(java.net.URI uri)
Creates a File object for the pathname represented by the given file:
URI (Uniform Resource Identifier). If the given URI is not a suitable file
URI then IllegalArgumentException is thrown.
Five "get" methods retrieve information about the components of a File object's pathname. The following
code invokes each of them after creating a File object for the file "FileInfo.java" in the "ok"
subdirectory of the parent of the current directory (specified by ".."):
File src = new File(".." + File.separator + "ok",
"FileInfo.java");
System.out.println("getName() = " + src.getName());
System.out.println("getPath() = " + src.getPath());
System.out.println("getAbsolutePath() = "
- src.getAbsolutePath());
System.out.println("getCanonicalPath() = " - src.getCanonicalPath());
System.out.println("getParent() = " + src.getParent());
And here is the output:
getName() = FileInfo.java
getPath() = ../ok/FileInfo.java
getAbsolutePath() = /vob/java_prog/src/../ok/FileInfo.java
getCanonicalPath() = /vob/java_prog/ok/FileInfo.java
getParent() = ../ok