简体   繁体   中英

Remove any possible line separator in the java properties file

I am currently trying to read from properties file having a single property in it that can have all possible line separators repeating any number of times eg

green.color.poem=Green is .... the color of spring.\r\nGreen is .... renewal.\n\nGreen is .... the color of envy.\r\n\r\nGreen is .... a new crayon.\\r\\nGreen tastes like .... a crisp apple.\\r\\n\\r\\nGreen smells like .... fresh cut grass.<br>Green sounds like .... a croaking frog.</p><p>Green feels like .... soft, velvety moss.\n\nGreen looks like .... shiny emeralds.\r\n\\r\\nGreen makes me .... go.\n\nGreen is .... my favorite color.

As could be seen above, the property value contains \\r, \\n, \\\\r, \\\\n, < br> and < / p> < p> as line separators repeating any number of times... I just need to split this property value (into a string array) based on the line separators so that the first array element would hold "Green is .... the color of spring.", the second element would hold "Green is .... renewal.", so on and so forth. I tried using java Properties class which can understand the \\r and \\n characters and split the value accordingly but it does not honor the \\\\r, \\\\n, < br> and < / p> < p> characters. How would I be able to enforce the program to treat the \\r, \\n, \\\\r and \\\\n in a same way??

You cannot load this file using the standard Properties functions as it is not a valid file. The first unescaped newline terminates the property and the rest is ignored.

What you need to do is read it as a plain file and reconstruct the desired value yourself.

I'd recommend handling this in 3 steps:

  1. Read the file into a list of strings, one per line. This takes care of the \\r\\n delimiters.
  2. For each line, remove all the extraneous markup ( <br> , <p></p> , etc)
  3. Concatenate the results into a single string and then create a property with that value.

You will not be able to this via the Properties class directly. You can use that to read in the property value. But you will then need to use either the StringTokenizer class or the split(String regex) method in the String class, or the Regular Expression API's Pattern & Matcher classes (which the String.split(String regex) class uses under the hood).

Read the property Value as using Properties .

Then run replaceAll() for all the type of delimiters that you want to remove. Then replace those with a single delimiter.

replaceAll("\r\n", "|");
replaceAll("\n\n", "|");
replaceAll("\r\n\r\n", "|");

...

Then split() the string based on the "|" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM