Space character without “” in properties file keys

Tags:



I reed properties file:

InputStream input = new FileInputStream("data.ini");
Reader reader = new InputStreamReader(input, Charset.forName("UTF-8"));
Properties prop = new Properties();
prop.load(reader);

My properties file Key contains space and I need to put character to read it correctly. Is it possible somehow not place character in properties file and read it in correct way?

Properties file content:

aaa bbb=0

Answer

It is possible to load properties from an XML file also… makes for neater code.

properties.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>Sample Properties file in XML</comment>
    <entry key="PropertyWithoutSpaces">Property value</entry>
    <entry key="Property With Spaces">Property value</entry>
</properties>

PropertyTest.java:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertyTest {

    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.loadFromXML( new FileInputStream("properties.xml") );

        System.out.println( properties.getProperty("Property With Spaces") );
        System.out.println( properties.getProperty("PropertyWithoutSpaces") );
    }

}


Source: stackoverflow