java中获取路径的方式

1.maven工程获取resource目录下文件路径的方法

获取当前测试或正式目录下文件的路径,一般src/test/resouces的目录也就是类的编译路径,src/test/resouces目录下的文件会编译在target\test-classes目录下

String path= Init.class.getClassLoader().getResource("./").getPath();
// 获取当前路径下类的编译路径
String path = Init.class.getClassLoader().getResource("文件名").getPath();
// 获取该文件所在路径(此时文件在resource文件夹下面)

猜你喜欢

转载自blog.csdn.net/potatobeancox55555/article/details/79721213