All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Read YAML File In Java

Last Updated : Mar 11, 2024

How To Read YAML File In Java

In this article we will show you the solution of how to read yaml file in java, the data-serialization language known as YAML, or YAML Ain't Markup Language, is most frequently used to provide project configuration information.

The primary driving force behind YAML is that it is created in a human-friendly way.

We can quickly comprehend the properties, their associated values, and, if applicable, any relationships between the properties.

The load() method can be used to load a single document, while the loadAll() method can load multiple documents at once.

We will now discuss the idea of how to read yaml files in java with an example.

Step By Step Guide On How To Read YAML File In Java :-

package Configuration;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
public class TC{
    public static void main(String[] args) throws FilenotfoundException {
        Yaml yaml = new Yaml();
        InputStream inputStream = new FileInputStream("path.yml");
        System.out.println(inputStream);
        HashMap yamlMap = yaml.load(inputStream);
        for (Object o : yamlMap.entrySet()) {
            System.out.println(o);
        }
        HashMap products = (HashMap) yamlMap.get("products");
        HashMap product = (HashMap) products.get("ProductA");
        HashMap suite = (HashMap) product.get("suite");
        HashMap suiteName = (HashMap) suite.get("SuiteName_B");
        ArrayList environment = (ArrayList) suiteName.get("environment_1");
        System.out.println(environment);
    }
}
  1. Import necessary libraries In this class, we are using the SnakeYAML library to parse the YAML file.
  2. Define a main method We will be running the code from the main method.
  3. Create an instance of Yaml We create an object of the Yaml class to use its load method to parse the YAML file.
  4. Create an InputStream object to read the YAML file We use a FileInputStream object to read the file from the specified path.
  5. Load the YAML into a Map We use the load method of the Yaml object to parse the YAML content into a Map object.
  6. Iterate over the Map object We use a for loop to iterate over the Map object and print out each key-value pair.
  7. Access HashMaps and ArrayLists We use the get method of the HashMap objects to access nested HashMaps and ArrayLists.
  8. Print the contents of the ArrayList We use the println method to output the contents of the ArrayList to the console.

Conclusion :-

As a result, we have successfully learned how to read yaml files in java with an example.

The ability to manage YAML files using code is becoming more and more important as they are used more frequently, particularly for providing project properties and build and deployment meta-data.

SnakeYAML allows us to manage YAML files in our Java project with ease, and it only requires a small bit of code to load YAML files into our project or write data into YAML files.

Additionally, SnakeYAML offers formatting options so you can adjust and personalize it to suit your needs.

I hope this article on how to read yaml file in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪