I'm writing a simple parser to convert a java properties file which contains entries in name=value
pair to a json
string.
Below is the code. The code requires that each entry is in a new line:
sCurrentLine = br.readLine();
while ((sCurrentLine) != null)
{
config+=sCurrentLine.replace('=', ':');
sCurrentLine = br.readLine()
if(sCurrentLine!=null)
config+=",";
}
config+="}";
The function works fine except in cases when there are extra empty new lines in the properties file. (Ex: Suppose I write the last entry in the props file and then hit two enters, the file will contain two empty new lines after the last entry) . While the expected output is {name1:value1,name2:value2}
, in the above case when extra new lines are present , I get the output as {name1:value1,name2:value2,}
. The number of trailing ,
increases with the number of empty new lines.
I know its because the readLine()
reads the empty line while logically it shouldn't but how do I change this?