File 路径

File 路径

By default the classes in the java.io package always resolve relative pathnames against the
current user directory. This directory is named by the system property user.dir

    /**
     * 获取当前项目的路径
     * D:\workspace_for_idea\Demo\Test
     */
    @Test
    public void testSystemPro(){
        System.out.println(System.getProperty("user.dir"));
    }

As Follow

 	/**
     * File的相对路径是项目路径 Content Root
     */
    @Test
     public void testFile(){
        //获取当前的项目目录 D:\workspace_for_idea\Demo\Test\
        System.out.println(new File("").getAbsolutePath());//等价于new File(".")

        //获取当前项目目录下的test.txt D:\workspace_for_idea\Demo\Test\test.txt
        System.out.println(new File("test.txt").getAbsolutePath());

        //获取根分路径 D:\
        System.out.println(new File("/").getAbsolutePath());
    }
posted @ 2020-08-13 23:34  CyberPelican  阅读(158)  评论(0编辑  收藏  举报