properties文件加载的六种方法

摘要:
加载项目properties文件的六种方法,其中四种都是通过Properties类加载inputStream读取,后两种通过ResourcesBundle类和其子类来加载/***通过inputStream加载配置文件到properties对象*/privatevoidgetPropertiesByInputStream_One()throwsIOException{//全路径Stringpath=

加载项目properties文件的六种方法,其中四种都是通过Properties类加载inputStream读取,后两种通过ResourcesBundle类和其子类来加载

/*** 通过inputStream加载配置文件到properties对象
     */

    private void getPropertiesByInputStream_One() throwsIOException {
        //全路径
        String path = "/Users/grahamliu/idea-workspace/AppiumAIDemo/src/main/resources/appium.properties";
        Properties properties = newProperties();

        InputStream inputStream = new BufferedInputStream(newFileInputStream(path));
        properties.load(inputStream);

        System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());

        System.out.println(properties.getProperty("appiumUrl"));
    }

    /*** 通过class类getResourceAsStream方法加载配置文件流
     */
    private void getPropertiesByInputStream_Two() throwsIOException {
        //路径/开头,表示从classpath下取路径
//String path = "/appium.properties";
        //路径不为/开头,从当前类所在包下取
        String path = "appiumRelative.properties";

        Properties properties = newProperties();
        InputStream inputStream = this.getClass().getResourceAsStream(path);
        properties.load(inputStream);

        System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());

        System.out.println(properties.getProperty("appiumUrl"));
    }

    /***通过class的类加载器getClassLoader加载配置
     */
    private void getPropertiesByInputStream_Three() throwsIOException{
        //getClassLoader默认加载路径就是classpath,规定不需要用/开头文件路径
        String path = "appium.properties";

        Properties properties= newProperties();
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
        properties.load(inputStream);

        System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());

        System.out.println(properties.getProperty("appiumUrl"));
    }

    /*** 通过ClassLoader类静态方法加载
* 2020.11.3 最新经验,该方法可能在web项目,或者mvn -exec:运行时读取不到配置文件,需要用上面的方法
*/ private void getPropertiesByInputStream_Four() throwsIOException{ //ClassLoader默认加载路径就是classpath,规定不需要用/开头文件路径 String path = "appium.properties"; Properties properties = newProperties(); InputStream inputStream =ClassLoader.getSystemResourceAsStream(path); properties.load(inputStream); System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.println(properties.getProperty("appiumUrl")); } /*** 通过ResourceBundle的构造方法getBundle */ private voidgetPropertiesByResourceBundle_Five(){ //这个getBundle()方法的参数相对同目录路径,并去掉.properties后缀,否则将抛异常 String path = "appium"; ResourceBundle resourceBundle =ResourceBundle.getBundle(path); System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.println(resourceBundle.getString("appiumUrl")); } /*** 通过ResourceBundle子类PropertyResourceBundle加载inputStream */ private void getPropertiesByResourceBundle_Six() throwsIOException{ String path = "/Users/grahamliu/idea-workspace/AppiumAIDemo/src/main/resources/appium.properties"; InputStream inputStream = new BufferedInputStream(newFileInputStream(path)); ResourceBundle resourceBundle = newPropertyResourceBundle(inputStream); System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.println(resourceBundle.getString("appiumUrl")); }

免责声明:文章转载自《properties文件加载的六种方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vmware虚拟机上网银行怎样处理坏账和贷款展期下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring Boot之多环境设置及切换

一、使用properties设置多环境 创建多个properties文件; application-dev.properties:(开发环境) server.port=8883 application-test.properties:(测试环境) server.port=8884 appliction.properties:(主环境) server.p...

【转】Appium如何定位安卓APP元素

转载原文:https://www.jianshu.com/p/efe9dcf8bbaf 一、定位工具 在安装appium环境的时候我们已经安装了SDK,里面就自带有元素定位的工具,位置在.../sdk/tools/uiautomatorviewer.bat,由于安装路径各有不同,如果实在找不到可以具体路径,可以在电脑中查找uiautomatorvie...

appium基础一:连接手机和appium-desktop定位元素

一、获取手机信息 adb devices获取手机或模拟器的设备名 aapt dump badging xxxx.apk获取app的包名、入口等基本信息 如果想要知道手机原生携带的app的基本信息,首先是无法获取到这个apk软件包,也就无法用aapt的方法来获取对应的信息。此时可以通过抓取日志的方法获取基本信息。 举例:想要获取到手机功能---设置  的...

yml在线格式转换工具(properties)

分享一个在线properties转yml工具,也支持yml转properties, 域名非常简单好记,直接在地址栏里输入toyaml.com, 地址:http://toyaml.com/ yml,即yaml文本格式文件的后缀名,yaml可以用来替代properties,可读性更好一些。 目前springboot的配置文件已经同时支持properties和y...

ASP.NET Core中添加MIME 类型

目录 #事故现场 #解决方法 #事故现场 在asp.net core 中使用pdf.js插件,然后遇到一个问题,发现pdf的工具条的文字都是英文的;打开浏览器的控制台,发现有一个报错:http://localhost:2076/lib/pdf.js/web/locale/zh-CN/viewer.properties这个文件404了。而且还报了很...

Java创建数据库连接池(两种方式)

提供数据库连接池:作用是提供动态的连接闭合状态(即用即开、不用即关、提高效率)              提供两个静态方法:①OpenConn()②CloseConn(Connection conn)1.使用Propertie对象获取数据库配置文件(即Jdbc.properties)提供的配置信息以供给第二步使用2.数据库目前我了解的有两种创建连接的方式=...