概述
使用java代码读取properties文件,在new File的时候突然发现直接使用相对路径不管用了,之前做开发因为业务以及工作内容没有涉及到这块,也没有太关注这方面的东西,这次做一个相对比较全的分析。
1、可以通过类的class文件路径获知当前项目或者编译文件的路径
在Class类里面有这样一个方法getResource(String name)
查找带有给定名称的资源获取一个URL


public static void main(String[] args) throws IOException { //获取当前类class所在的resource路径 System.out.println(TestFilePath.class.getResource("/").getPath()); //获取当前类class所在的路径: System.out.println(TestFilePath.class.getResource("").getPath()); }
代码输出结果如下,我们可以很明显的知道加载资源文件是需要resource根目录的,一般我们使用带斜杠的
/E:/develop/workspace/selfproject/nutrition/target/test-classes/ /E:/develop/workspace/selfproject/nutrition/target/test-classes/cn/test/
获取这个路径后还要直接拼接资源文件路径,可以直接通过TestFilePath.class.getResource("/request-mapping.properties").getPath()方式获取
2、通过new File("")来确定工程目录(非编译运行文件的目录)


public static void main(String[] args) throws IOException { File f = new File(""); System.out.println("空字符file的Path : " + f.getPath()); System.out.println("空字符file的标准路径CanonicalPath : " + f.getCanonicalPath()); System.out.println("空字符file的绝对路径AbsolutePath : " + f.getAbsolutePath()); }
代码输出结果如下,可以根据资源文件在工程目录对应的路径进行资源路径的拼接,从而完成资源的定位(同时也可以通过直接new File(path)的方式获取文件,其中path是相对于工程目录的路径)
空字符file的Path :
空字符file的正则路径CanonicalPath : E:developworkspaceselfprojectnutrition
空字符file的绝对路径AbsolutePath : E:developworkspaceselfprojectnutrition
3、通过类加载的路径来获取对应的路径信息


public static void main(String[] args) throws IOException { URL xmlpath = new TestFilePath().getClass().getClassLoader().getResource("/"); URL xmlpath1 = new TestFilePath().getClass().getClassLoader().getResource(""); System.out.println("获取当前类被加载的工程路径:" + xmlpath); System.out.println("获取当前类被加载的路径:" + xmlpath1); }
代码输出结果如下,结果说明类加载获取resource时只能用getResource(""),这是因为加载类的路径就默认是在根目录下的,加斜杠反而无法获取路径(将斜杠当成资源名称了)
获取当前类被加载的工程路径:null 获取当前类被加载的路径:file:/E:/develop/workspace/selfproject/nutrition/target/test-classes/
4、直接通过java的系统System类获取项目路径


public static void main(String[] args) throws IOException { System.out.println(System.getProperty("user.dir")); }
代码输出结果如下,获取的是项目的根目录
E:developworkspaceselfprojectnutrition
补充,这里可以通过system获取的对应信息可以都了解下


public static void main(String[] args) throws IOException { Properties p = System.getProperties(); Set<Entry<Object, Object>> entrys = p.entrySet(); for (Entry<Object, Object> entry : entrys) { System.out.println(entry.getKey().toString() + " : " + entry.getValue().toString()); } }
打印结果:


java.runtime.name : Java(TM) SE Runtime Environment sun.boot.library.path : C:Program FilesJavajdk1.7.0_79jrebin java.vm.version : 24.79-b02 java.vm.vendor : Oracle Corporation java.vendor.url : http://java.oracle.com/ path.separator : ; java.vm.name : Java HotSpot(TM) 64-Bit Server VM file.encoding.pkg : sun.io user.country : CN user.script : sun.java.launcher : SUN_STANDARD sun.os.patch.level : java.vm.specification.name : Java Virtual Machine Specification user.dir : E:developworkspaceselfprojectnutrition java.runtime.version : 1.7.0_79-b15 java.awt.graphicsenv : sun.awt.Win32GraphicsEnvironment java.endorsed.dirs : C:Program FilesJavajdk1.7.0_79jrelibendorsed os.arch : amd64 java.io.tmpdir : C:UsersAdminAppDataLocalTemp line.separator : java.vm.specification.vendor : Oracle Corporation user.variant : os.name : Windows 8.1 sun.jnu.encoding : GBK java.library.path : C:Program FilesJavajdk1.7.0_79bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:ProgramDataOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program Files (x86)IDM Computer SolutionsUltraEdit;C:Program FilesIDM Computer SolutionsUltraCompare;D:developnexus-2.14.5-02-bundlenexus-2.14.5-02bin;C:Program FilesIDM Computer SolutionsUltraEdit;D:developapache-maven-3.5.2bin;C:Program FilesJavajdk1.7.0_79bin;C:Program FilesJavajdk1.7.0_79jrebin;C:Program FilesMySQLMySQL Utilities 1.6;C:UsersAdminAppDataLocalMicrosoftWindowsApps;. java.specification.name : Java Platform API Specification java.class.version : 51.0 sun.management.compiler : HotSpot 64-Bit Tiered Compilers os.version : 6.3 user.home : C:UsersAdmin user.timezone : java.awt.printerjob : sun.awt.windows.WPrinterJob file.encoding : UTF-8 java.specification.version : 1.7 java.class.path : E:developworkspaceselfprojectnutritiontargettest-classes;E:developworkspaceselfprojectnutritiontargetclasses;D:developapache-maven-3.5.2self-repolog4jlog4j1.2.9log4j-1.2.9-sources.jar;D:developapache-maven-3.5.2self-repojavaxservletservlet-api2.5servlet-api-2.5.jar user.name : Admin java.vm.specification.version : 1.7 sun.java.command : cn.test.TestFilePath java.home : C:Program FilesJavajdk1.7.0_79jre sun.arch.data.model : 64 user.language : zh java.specification.vendor : Oracle Corporation awt.toolkit : sun.awt.windows.WToolkit java.vm.info : mixed mode java.version : 1.7.0_79 java.ext.dirs : C:Program FilesJavajdk1.7.0_79jrelibext;C:WINDOWSSunJavalibext sun.boot.class.path : C:Program FilesJavajdk1.7.0_79jrelibresources.jar;C:Program FilesJavajdk1.7.0_79jrelibrt.jar;C:Program FilesJavajdk1.7.0_79jrelibsunrsasign.jar;C:Program FilesJavajdk1.7.0_79jrelibjsse.jar;C:Program FilesJavajdk1.7.0_79jrelibjce.jar;C:Program FilesJavajdk1.7.0_79jrelibcharsets.jar;C:Program FilesJavajdk1.7.0_79jrelibjfr.jar;C:Program FilesJavajdk1.7.0_79jreclasses java.vendor : Oracle Corporation file.separator : java.vendor.url.bug : http://bugreport.sun.com/bugreport/ sun.io.unicode.encoding : UnicodeLittle sun.cpu.endian : little sun.desktop : windows sun.cpu.isalist : amd64
https://blog.csdn.net/theonegis/article/details/23695057
转载于:https://www.cnblogs.com/liushi805347332/p/9022139.html
最后
以上就是踏实短靴为你收集整理的【002】java关于resource资源文件路径的加载问题的全部内容,希望文章能够帮你解决【002】java关于resource资源文件路径的加载问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复