Java 实例 - 获取文件的上级目录


以下实例演示了使用 File 类的 file.getParent() 方法来获取文件的上级目录:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. import java.io.File;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. File file = new File("C:/File/demo.txt");
  11. String strParentDirectory = file.getParent();
  12. System.out.println("文件的上级目录为 : "
  13. + strParentDirectory);
  14. }
  15. }

以上代码运行输出结果为:

  1. 文件的上级目录为 : File