一.获取路径:
单独的Java类中获得绝对路径根据java.io.File的Doc文挡,可知: 默认情况下new File("/")代表的目录为:System.getProperty("user.dir")。
一下程序获得执行类的当前路径:
System.out.println("1:" + Test2.class.getResource("")); System.out.println("2:" + Test2.class.getClassLoader().getResource("")); System.out.println("3:" + Test2.class.getClassLoader().getResource("").getPath()); System.out.println("4:" +ClassLoader.getSystemResource(""));
输出结果为:
1:file:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/demo1/
2:file:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/
3:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/
4:file:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/