java获取项目的路径

来源:互联网 发布:杭州周边旅游知乎 编辑:程序博客网 时间:2024/06/05 20:29

public class TestGetPath {

@Testpublic void getCurrentPath(){    //取得根目录路径    //getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)    ///E:/myEclipse_workspace/Hxzs/build/classes/    String rootPath = getClass().getResource("/").getFile().toString();    System.out.println(rootPath);    //当前目录路径    ///E:/myEclipse_workspace/Hxzs/build/classes/com/honszeal/test/    ///E:/myEclipse_workspace/Hxzs/build/classes/com/honszeal/test/    ///E:/myEclipse_workspace/Hxzs/build/classes/com/honszeal/    String currentPath1 = getClass().getResource(".").getFile().toString();    System.out.println(currentPath1);    String currentPath2 = getClass().getResource("").getFile().toString();    System.out.println(currentPath1);    //当前目录的上级目录    String parentPath = getClass().getResource("../").getFile().toString();    System.out.println(parentPath);    //利用System.getProperty()函数获取当前路径,得到项目文件夹的根目录,不带/     //E:\myEclipse_workspace\Hxzs    System.out.println(System.getProperty("user.dir"));}
0 0
原创粉丝点击