Java implementation reads the Yaml file and gets the value

Source: Internet
Author: User

Start by creating a new Test.yaml file in the project SRC directory.

The code is as follows:

Spring:    application:        name:cruncher    DataSource:        driverClassName:com.mysql.jdbc.Driver        URL: Jdbc:mysql://localhost/testserver:    port:9000

Then the test code:

 PackageCom.ming.yaml;ImportJava.io.FileInputStream;ImportJava.net.URL;ImportJava.util.Map;ImportOrg.yaml.snakeyaml.Yaml; Public classTest { Public Static voidMain (string[] args) {Try{Yaml Yaml=NewYaml (); URL URL= Test.class. getClassLoader (). GetResource ("Test.yaml"); if(URL! =NULL) {                //gets the configuration data from the Test.yaml file, and then converts it to obj,Object obj =yaml.load (NewFileInputStream (Url.getfile ()));                System.out.println (obj); //You can also convert a value to a mapMap map = (map) yaml.load (NewFileInputStream (Url.getfile ()));                SYSTEM.OUT.PRINTLN (map); //we can take the value by map.                            }        } Catch(Exception e) {e.printstacktrace (); }                    }}

The results of the operation are as follows:

Summary: By getting the configuration properties of the Yaml file, you can take a value from map, and if you want to convert the obtained string to an object, you may go through the third-party plug-in library. This yaml file is really much more powerful than a property file.

Java implementation reads the Yaml file and gets the value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.