Table of Contents
1.Properties and ResourceBundle" >1.Properties and ResourceBundle
2.Properties" >2.Properties
3.ResourceBundle" >3.ResourceBundle
4. Read the properties file into the Spring container " >4. Read the properties file into the Spring container
5. Notes" > 5. Notes
6. Encoding" > 6. Encoding
Home Java javaTutorial Detailed introduction to Properties and ResourceBundle

Detailed introduction to Properties and ResourceBundle

Jul 20, 2017 am 10:37 AM
ie properties resourcebundle

1.Properties and ResourceBundle

Both classes can read the key-value pairs stored in the form of key/value in the property file. When ResourceBundle reads the property file The operation is relatively simple.

2.Properties

This class inherits Hashtable and stores key-value pairs in a collection. Based on the input stream, key-value pairs are read from the property file. After the load() method is called, it is disconnected from the input stream and will not automatically close the input stream. It needs to be closed manually.

    /** * 基于输入流读取属性文件:Properties继承了Hashtable,底层将key/value键值对存储在集合中,
     * 通过put方法可以向集合中添加键值对或者修改key对应的value
     * 
     * @throws IOException     */@SuppressWarnings("rawtypes")
    @Testpublic void test01() throws IOException {
        FileInputStream fis = new FileInputStream("Files/test01.properties");
        Properties props = new Properties();
        props.load(fis);// 将文件的全部内容读取到内存中,输入流到达结尾fis.close();// 加载完毕,就不再使用输入流,程序未主动关闭,需要手动关闭/*byte[] buf = new byte[1024];
        int length = fis.read(buf);
        System.out.println("content=" + new String(buf, 0, length));//抛出StringIndexOutOfBoundsException*/System.out.println("driver=" + props.getProperty("jdbc.driver"));
        System.out.println("url=" + props.getProperty("jdbc.url"));
        System.out.println("username=" + props.getProperty("jdbc.username"));
        System.out.println("password=" + props.getProperty("jdbc.password"));/** * Properties其他可能用到的方法         */props.put("serverTimezone", "UTC");// 底层通过hashtable.put(key,value)props.put("jdbc.password", "456");
        FileOutputStream fos = new FileOutputStream("Files/test02.xml");// 将Hashtable中的数据写入xml文件中props.storeToXML(fos, "来自属性文件的数据库连接四要素");

        System.out.println();
        System.out.println("遍历属性文件");
        System.out.println("hashtable中键值对数目=" + props.size());
        Enumeration keys = props.propertyNames();while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            System.out.println(key + "=" + props.getProperty(key));
        }

    }
Copy after login

3.ResourceBundle

This class reads the property file based on the class: treat the property file as a class, This means that the properties file must be placed in the package, and the properties file is referred to using its fully qualified class name rather than its path.

    /** * 基于类读取属性文件:该方法将属性文件当作类来处理,属性文件放在包中,使用属性文件的全限定性而非路径来指代文件     */@Testpublic void test02() {
        ResourceBundle bundle = ResourceBundle.getBundle("com.javase.properties.test01");
        System.out.println("获取指定key的值");
        System.out.println("driver=" + bundle.getString("jdbc.driver"));
        System.out.println("url=" + bundle.getString("jdbc.url"));
        System.out.println("username=" + bundle.getString("jdbc.username"));
        System.out.println("password=" + bundle.getString("jdbc.password"));

        System.out.println("-----------------------------");
        System.out.println("遍历属性文件");
        Enumeration<String> keys = bundle.getKeys();while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            System.out.println(key + "=" + bundle.getString(key));
        }
    }
Copy after login

4. Read the properties file into the Spring container

Usually the four elements of database connection are placed in the properties file , the program reads parameters from the property file, so that when the database connection elements change, does not need to modify the source code. Method to load the content in the properties file into the xml document:

  1. Configure context constraints in the configuration file header.

  2. Add to the configuration file and load the properties to in the configuration file.

  3. Get configuration file content: ${key}

5. Notes

# placed in front is used to add comments in the properties file.

6. Encoding

The attribute file adopts ISO-8859-1 encoding method. This encoding method does not support Chinese and Chinese characters will be converted. Displayed in Unicode encoding mode.

The above is the detailed content of Detailed introduction to Properties and ResourceBundle. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Internet Explorer opens Edge: How to stop MS Edge redirection Internet Explorer opens Edge: How to stop MS Edge redirection Apr 14, 2023 pm 06:13 PM

It's no secret that Internet Explorer has fallen out of favor for a long time, but with the arrival of Windows 11, reality sets in. Rather than sometimes replacing IE in the future, Edge is now the default browser in Microsoft's latest operating system. For now, you can still enable Internet Explorer in Windows 11. However, IE11 (the latest version) already has an official retirement date, which is June 15, 2022, and the clock is ticking. With this in mind, you may have noticed that Internet Explorer sometimes opens Edge, and you may not like it. So why is this happening? exist

What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) Feb 10, 2024 am 10:30 AM

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

How to read custom properties configuration file under SpringBoot How to read custom properties configuration file under SpringBoot May 13, 2023 pm 10:43 PM

1. Create a new .properties file in resource. Create a new config folder in the resource directory, and then create a new .properties file and place it in the folder. As shown in the figure remote.properties 2. Write the configuration file remote.uploadFilesUrl=/resource/files/remote.uploadPicUrl=/resource/pic/ 3. Create a new configuration class RemoteProperties.java@Configuration@ConfigurationProperties(

6 ways to read properties files, recommended to collect! 6 ways to read properties files, recommended to collect! Aug 15, 2023 pm 04:03 PM

These days, we basically use Spring Boot for development, and we all know that there will be an application.properties configuration file in the project (some are also application.yaml, which is used to save some of our configuration information anyway). Usually we put some configuration files Information is written into the properties file, such as: database connection information, third-party interface information (key, username, password, address, etc.), connection pool, Redis configuration information, various third-party component configuration information

How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page Mar 20, 2024 pm 09:21 PM

Recently, many win10 users have found that their IE browser always automatically jumps to the edge browser when using computer browsers. So how to turn off the automatic jump to edge when opening IE in win10? Let this site carefully introduce to users how to automatically jump to edge and close when opening IE in win10. 1. We log in to the edge browser, click... in the upper right corner, and look for the drop-down settings option. 2. After we enter the settings, click Default Browser in the left column. 3. Finally, in the compatibility, we check the box to not allow the website to be reloaded in IE mode and restart the IE browser.

How does SpringBoot read YML, yaml, and properties files? How does SpringBoot read YML, yaml, and properties files? May 13, 2023 pm 09:07 PM

Yml, yaml, and properties files are all used to store configuration files. Some static data and configuration data will be stored in them. But sometimes we not only need to store static data, but also need to read data from files. These three types of files are placed under project--"src--"main--"resource. If other files need to be stored in the resource, a config package will be created in the resource. Store the configuration file in it. 1. Create a new configuration file in the project. The file storage address bootstrap.yml is a configuration file of the SpringBoot program. It will be loaded before the project starts.

The end of an era: Internet Explorer 11 is retired, here's what you need to know The end of an era: Internet Explorer 11 is retired, here's what you need to know Apr 20, 2023 pm 06:52 PM

June 15, 2022 is the day when Microsoft ends support for Internet Explorer 11 (IE11) and closes its legacy browser chapter. The company has been reminding users of this end-of-life date for some time and calling on them to plan a move to Microsoft Edge. Microsoft bundles IE11 with Windows 8.1 as the modern default web browser for Windows. Although it never reached the (current) heights of Chrome, it was the second most used desktop browser in 2014, behind IE8. Of course, with 20

How to solve the problem that IE shortcut cannot be deleted How to solve the problem that IE shortcut cannot be deleted Jan 29, 2024 pm 04:48 PM

Solutions to IE shortcuts that cannot be deleted: 1. Permission issues; 2. Shortcut damage; 3. Software conflicts; 4. Registry issues; 5. Malicious software; 6. System issues; 7. Reinstall IE; 8. Use third-party tools; 9. Check the target path of the shortcut; 10. Consider other factors; 11. Consult professionals. Detailed introduction: 1. Permission issue, right-click the shortcut, select "Properties", in the "Security" tab, make sure you have sufficient permissions to delete the shortcut. If not, you can try running as an administrator, etc.

See all articles