public static void main(String[] args){ try { File f = new File("testFile.txt"); File abstractf = new File("..\\testFile.txt"); File emptyf = new File(""); System.out.println("f's abstract path:" + f.getPath()); System.out.println("f.getAbsolutePath():" + f.getAbsolutePath()); System.out.println("f.getCanonicialPath():" + f.getCanonicalPath()); System.out.println("abstractf's abstract path:" + abstractf.getPath()); System.out.println("abstractf.getAbsolutePath():" + abstractf.getAbsolutePath()); System.out.println("abstractf.getCanonicialPath():" + abstractf.getCanonicalPath()); System.out.println("emptyf's abstract path:" + emptyf.getPath()); System.out.println("emptyf.getAbsolutePath():" + emptyf.getAbsolutePath()); System.out.println("emptyf.getCanonicialPath():" + emptyf.getCanonicalPath()); File newFile = new File(emptyf,"newFile.txt"); System.out.println("-------------"); System.out.println("newFile's abstract path: " + newFile.getPath()); System.out.println("newFile's absolute path: " + newFile.getAbsolutePath()); System.out.println("newFile's canonicial path: " + newFile.getCanonicalPath()); FileWriter newWriter = new FileWriter(newFile); newWriter.write("new test data"); newWriter.flush(); } catch (IOException e) { e.printStackTrace(); } }
输出:
f's abstract path:testFile.txt
f.getAbsolutePath():D:\springWorkSpace\Fortest\testFile.txt
f.getCanonicialPath():D:\springWorkSpace\Fortest\testFile.txt
abstractf's abstract path:..\testFile.txt
abstractf.getAbsolutePath():D:\springWorkSpace\Fortest\..\testFile.txt
abstractf.getCanonicialPath():D:\springWorkSpace\testFile.txt
emptyf's abstract path:
emptyf.getAbsolutePath():D:\springWorkSpace\Fortest
emptyf.getCanonicialPath():D:\springWorkSpace\Fortest
-------------
newFile's abstract path: \newFile.txt
newFile's absolute path: D:\newFile.txt
newFile's canonicial path: D:\newFile.txt