https://docs.oracle.com/javase/1.5.0/docs/api/java/io/File.html
public class FilePathTest { public static void main(String[] args) { try { File f = new File("test.xml"); System.out.println("----------File set to :test.xml----------"); System.out.println("getAbsolutePath:" + f.getAbsolutePath()); System.out.println("getCanonicalPath:" + f.getCanonicalPath()); System.out.println("getName:" + f.getName()); System.out.println("getPath:" + f.getPath()); System.out.println("getParent:" + f.getParent()); System.out.println("getParentFile:" + f.getParentFile()); if (f.getParentFile() != null) { System.out.println("getParentFile().getAbsolutePath():" + f.getParentFile().getAbsolutePath()); System.out.println("getParentFile().getCanonicalPath():" + f.getParentFile().getCanonicalPath()); } f = new File("D:\\eclipse-ws\\20130307_WEB\\TestJava\\test.xml"); System.out.println("\n" + "----------File set to :D:\\eclipse-ws\\20130307_WEB\\TestJava\\test.xml----------"); System.out.println("getAbsolutePath:" + f.getAbsolutePath()); System.out.println("getCanonicalPath:" + f.getCanonicalPath()); System.out.println("getName:" + f.getName()); System.out.println("getPath:" + f.getPath()); System.out.println("getParent:" + f.getParent()); if (f.getParentFile() != null) { System.out.println("getParentFile().getAbsolutePath():" + f.getParentFile().getAbsolutePath()); System.out.println("getParentFile().getCanonicalPath():" + f.getParentFile().getCanonicalPath()); System.out.println("getParentFile().getName():" + f.getParentFile().getName()); System.out.println("getParentFile().getPath():" + f.getParentFile().getPath()); System.out.println("getParentFile().getParent():" + f.getParentFile().getParent()); } f = new File("test\\..\\.\\test.xml"); System.out.println("\n" + "----------File set to :test\\..\\.\\test.xml----------"); System.out.println("getAbsolutePath:" + f.getAbsolutePath()); System.out.println("getCanonicalPath:" + f.getCanonicalPath()); System.out.println("getName:" + f.getName()); System.out.println("getPath:" + f.getPath()); System.out.println("getParent:" + f.getParent()); if (f.getParentFile() != null) { System.out.println("getParentFile().getAbsolutePath():" + f.getParentFile().getAbsolutePath()); System.out.println("getParentFile().getCanonicalPath():" + f.getParentFile().getCanonicalPath()); System.out.println("getParentFile().getName():" + f.getParentFile().getName()); System.out.println("getParentFile().getPath():" + f.getParentFile().getPath()); System.out.println("getParentFile().getParent():" + f.getParentFile().getParent()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
-------------------OUTPUT------------------------------------ ----------File set to :test.xml---------- getAbsolutePath:D:\eclipse-ws\20130307_WEB\TestJava\test.xml getCanonicalPath:D:\eclipse-ws\20130307_WEB\TestJava\test.xml getName:test.xml getPath:test.xml getParent:null getParentFile:null ----------File set to :D:\eclipse-ws\20130307_WEB\TestJava\test.xml---------- getAbsolutePath:D:\eclipse-ws\20130307_WEB\TestJava\test.xml getCanonicalPath:D:\eclipse-ws\20130307_WEB\TestJava\test.xml getName:test.xml getPath:D:\eclipse-ws\20130307_WEB\TestJava\test.xml getParent:D:\eclipse-ws\20130307_WEB\TestJava getParentFile().getAbsolutePath():D:\eclipse-ws\20130307_WEB\TestJava getParentFile().getCanonicalPath():D:\eclipse-ws\20130307_WEB\TestJava getParentFile().getName():TestJava getParentFile().getPath():D:\eclipse-ws\20130307_WEB\TestJava getParentFile().getParent():D:\eclipse-ws\20130307_WEB ----------File set to :test\..\.\test.xml---------- getAbsolutePath:D:\eclipse-ws\20130307_WEB\TestJava\test\..\.\test.xml getCanonicalPath:D:\eclipse-ws\20130307_WEB\TestJava\test.xml getName:test.xml getPath:test\..\.\test.xml getParent:test\..\. getParentFile().getAbsolutePath():D:\eclipse-ws\20130307_WEB\TestJava\test\..\. getParentFile().getCanonicalPath():D:\eclipse-ws\20130307_WEB\TestJava getParentFile().getName():. getParentFile().getPath():test\..\. getParentFile().getParent():test\..
- getPath() :will return the absolute or relative path of the File object, depending on which it was constructed with.
- getAbsolutePath(): will return the current directory name concatenated with the file separator character and the path name of the file
- getCanonicalPath(): will return a path name with the relative references resolved.