File file = new File("a.txt"); String parentPath = file.getParent(); // null File parentDir = file.getParentFile(); // null System.out.println("parentPath="+parentPath); // 获取相对路径 file = new File("D:/zhxn\a.txt"); parentPath = file.getParent(); // D:zhxn parentDir = file.getParentFile(); // D:zhxn System.out.println("parentPath="+parentPath); parentPath = parentDir.getParent(); // D: parentDir = parentDir.getParentFile(); // D: System.out.println("parentPath="+parentPath); parentPath = parentDir.getParent(); // null parentDir = parentDir.getParentFile(); // null System.out.println("parentPath="+parentPath);