String getParent String getName File getParentFile 描述 该java.io.File.getParent()方法返回的路径名字符串,如果此抽象路径名的家长或空,如果此路径名没有指定父目录。 声明 以下是java.io.File.getParent()方法的声明 public String getParent() 参数 NA 返回值 此方法返回此抽象路径名指定的父目录的路径名字符串,如果路径名未指定父路径,则返回null。 异常 NA 实例 以下示例显示了java.io.File.getParent()方法的用法。 package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; String v; boolean bool = false; try { // create new file f = new File("C:\\test.txt"); // get file name or directory name v = f.getParent(); // true if the file path exists bool = f.exists(); // if file exists if(bool) { // prints System.out.print("parent name: "+v); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 让我们编译并运行上面的程序,这将产生以下结果 parent name: C:\ String getName File getParentFile