In this tutorial, How to read and write a properties file content in Nodejs Application.

Usually, Properties files are used to store environment information as well as settings related to users or environment.

Nodejs has properties-reader npm library to read and write the property files.

  • Create a folder nodejs-reader
  • Inside a folder, Run npm init command to crate a nodejs scalfolding application
npm init -Y

This will create a basic nodejs folder Next, Install properties-reader npm library

npm install properties-reader

Let’s see an read and write properties file in nodejs application

Read properties file in Nodejs Application

In this example, you will learn how to Read key and its values from an properties file and display to console

Let’s declare properties file

database=mysql
hostname=localhost
username=john
password=

Here is an example and step by sep

  • Create a new javascript file - propertiesreader.js
  • import properties-reader module into javascript using require method.
  • Create an object for PropertiesReader with giving properties file location
  • This object has all properties key and values
  • You can get value using get method with given key and printed to console.

propertiesreader.js:

var PropertiesReader = require('properties-reader');
var properties = PropertiesReader('config/database.properties');

console.log(properties.get("database"));
console.log(properties.get("hostname"));
console.log(properties.get("username"));
console.log(properties.get("password"));

Output:

mysql
localhost
john

  • How to iterate or loop all key and values in a properties file?

It has each method to iterate all objects in a file

properties.each((key, value) => {
    console.log(key + ":" + value);
});

Output:

database:mysql
hostname:localhost
username:john
password:

How to write key and values to properties file in javascript

This example examples read properties in java with line by line

  • created a File object with absolute path
  • Create BufferedReader using FileReader object
  • get First line of properties file using readLine of BufferedReader
  • Loop using while loop until end of line reached
  • Print each line
var PropertiesReader = require('properties-reader');
const propertiesPah = "config/database.properties";
const properties = PropertiesReader(propertiesPah, { writer: { saveSections: true } });

properties.set("key1", "value1")
properties.set("database", "postgres")
properties.save(propertiesPah, function then(err, data) {
    if (err) {
        console.log("error in write a properties file")

    }
    console.log("saved data to properties file")

});






How to write a key and values to an properties file in java

In this example, You can read and write an properties using

  • Import PropertiesReader into javascript
  • Create a writer object using PropertiesReader with path and writer object
    • You can add new properties or update existing properties if properties file exists
  • set method do update or add key and values
  • save method of properties object writes to properties file.

Here is an complete example read and write an properties file

var PropertiesReader = require('properties-reader');
const propertiesPath = "config/database.properties";
const writer = PropertiesReader(propertiesPath, { writer: { saveSections: true } });

console.log(writer.get("database"));
console.log(writer.get("hostname"));
console.log(writer.get("username"));
console.log(writer.get("password"));

writer.each((key, value) => {
    console.log(key + ":" + value);
});

writer.set("key1", "value1")
writer.set("database", "value1")

writer.save(propertiesPath, function then(err, data) {
    if (err) {
        console.log("error in write a properties file")

    }
    console.log("saved data to properties file")

});

Output of an file

database=postgres
hostname=localhost
username=john
password=
key1=value1

Conclusion

You learned read a properties file with key and values as well and also write key and values to properties file in NodeJS javascript application