0

I have written a code wherein i am reading a properties file and adding the values to a hashmap and using it in a specific format to call an API. Problem is slash space (\ ) is replaced with a space. Adding the code:

public class propertfile {

    public static void main(String[] args) {
        FileInputStream fis;
        try {
            fis = new FileInputStream("path_to_properties_file");
            ResourceBundle resources = new PropertyResourceBundle(fis);
            Map<String, String> map = new HashMap<String, String>();

            //convert ResourceBundle to Map
            Enumeration<String> keys = resources.getKeys();

            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                map.put(key, resources.getString(key));
            }
            //Now you can use the 'map' object as you wish.
            Iterator < Entry<String, String> > new_Iterator = map.entrySet().iterator();
            JSONArray jsonArray = new JSONArray();

            while (new_Iterator.hasNext()) {
                JSONObject jo = new JSONObject();
                Map.Entry<String, String> new_Map = (Map.Entry<String, String> )
                new_Iterator.next();

                JSONObject requestParams = new JSONObject();
                requestParams.put("name", new_Map.getKey());
                requestParams.put("value", new_Map.getValue());

                jsonArray.put(requestParams);
            }

            System.out.println(jsonArray);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Actual properties file :

databases.my1_analysis.folder.1..\ db\ connection.datasources.custom.ds_sap_bap.Enable\ SNC=false

Desired with missing slash space: (Notice after the number 1.. , after db and also after Enable)

{"name": "databases.my1_analysis.folder.1.. db connection.datasources.custom.ds_sap_bap.Enable SNC", "value": "false"}

Any help is highly appreciated.

zforgo
  • 2,508
  • 2
  • 14
  • 22
User1515
  • 11
  • 7
  • Try to add another back slash. Like \\ instead of \ This is so-called escape character. – Mar-Z Mar 27 '23 at 19:39
  • Hello @Mar-Z thanks but i cant do that to the properties file, its not accessible for edit – User1515 Mar 27 '23 at 19:42
  • Do not use ResourceBundle to read a properties file. Use [Properties.load](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/Properties.html#load(java.io.Reader)) for that. ResourceBundle is for internationalized presentation data. – VGR Mar 28 '23 at 14:35

1 Answers1

0

Your program properly converts the contents of the Java properties file to JSON. The reason for the "removal" of the backslashes is the fact that the space character needs to be escaped with a backslash in the keys in the properties file, but not in JSON.

So the actual key stored in the property file shown in the question is databases.my1_analysis.folder.1.. db connection.datasources.custom.ds_sap_bap.Enable SNC, which is exactly the same value as in the JSON. The backslashes are just required to store the spaces in the properties files, as documented here. When reading the properties file using the PropertyResourceBundle class, the backslashes are removed as specified by the properties file format.

If you really want the backslashes to be preserved (which actually means that you change the key value), you can either re-add them after reading the properties file by putting a backslash before each space, or by implementing your own parser for the properties file.

Steffen D.
  • 71
  • 3