Escaping - character when reading from Java properites

Escaping - character when reading from Java properites



I'm trying to read in a - value from a Java properties file, but it keeps giving me the value â and something like an epsilon (having issues pasting it)



I have no issues escaping the " but it seems like no matter what I do - is giving me issues. I've tried '-' and - and \- but nothing seems to be working.


@Test
public void readFromProperties() throws IOException
Properties options = new Properties();

FileInputStream in = new FileInputStream(Configs.optionsFi);
negComments.load(in);
in.close();

String option = options.getProperty("OPTION8");
System.out.println(negComment);



properties file:


OPTION8=asdf asdf asdf asdf asdf – "asdfasdf"



System.out.println result:


asdf asdf asdf asdf asdf âepsilonlikething "asdfasdf"





in your example you have option equal to the good string value but the system.out.println is a weird result. However what you are printing in system isn't option but something called negComment. Why is that? where is it definedcreated?
– RAZ_Muh_Taz
Aug 30 at 23:04



option


option


negComment




1 Answer
1



The character in the property file is not a normal dash ('HYPHEN-MINUS' (U+002D)), but an 'EN DASH' (U+2013).




Documentation of load(InputStream inStream) explicitly specifies:


load(InputStream inStream)



The input stream is in a simple line-oriented format as specified in load(Reader) and is assumed to use the ISO 8859-1 character encoding; that is each byte is one Latin1 character.



Your property file is in UTF-8, so you get that character encoding error.



There are 3 ways to fix the problem:



Assuming that you intended a normal dash, replace the with a -,

and make sure you save the file in ISO 8859-1 aka Latin1 aka Windows-1252.



-



Encode the character, i.e. replace with u2013,

and make sure you save the file in ISO 8859-1 aka Latin1 aka Windows-1252.



u2013



Read the file using UTF-8:


try (BufferedReader in = Files.newBufferedReader(Paths.get(Configs.optionsFi)))
options.load(in);





Thank you! Encodings just drive me crazy. Option 3 worked as expected.
– farnett
Aug 31 at 14:59



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)