Avoiding line breaks (\ n) while reading and writing files

I read several property files to compare them with a template file for missing keys.

FileInputStream compareFis = new FileInputStream(compareFile); Properties compareProperties = new Properties(); compareProperties.load(compareFis); 

Note. I am reading a template file in the same way.

After reading, I compare them and write the missing keys with my values โ€‹โ€‹from the template file in Set.

 CompareResult result = new CompareResult(Main.resultDir); [...] if (!compareProperties.containsKey(key)) { retVal = true; result.add(compareFile.getName(), key + "=" + entry.getValue()); } 

Finally, I write the missing keys and their values โ€‹โ€‹to a new file.

 for (Entry<String, SortedSet<String>> entry : resultSet) { PrintWriter out = null; try { out = new java.io.PrintWriter(resultFile); SortedSet<String> values = entry.getValue(); for (String string : values) { out.println(string); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { out.flush(); out.close(); } } 

If I open the result file, I see that all line breaks "\ n" from the values โ€‹โ€‹of the template file are replaced with a new line. Example:

 test.key=Hello\nWorld! 

becomes

 test.key=Hello World! 

Although this is mostly correct, but in my case I have to save "\ n".

Does anyone know how I can avoid this?

+4
source share
6 answers

Since it seems that your output is a properties file, you should use Properties.store () to create the output file. This will not only take care of encoding newline characters, but also other special characters (e.g. characters other than ISO8859-1).

+2
source

Using println terminates each line with a line terminator for a particular platform. Instead, you can write a string delimiter that you want explicitly:

 for (Entry<String, SortedSet<String>> entry : resultSet) { PrintWriter out = null; try { out = new java.io.PrintWriter(resultFile); SortedSet<String> values = entry.getValue(); for (String string : values) { out.print(string); // NOT out.println(string) out.print("\n"); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { out.flush(); out.close(); } } 
+1
source

To add an example to JB Nizet's answer (the best I think) using Properties.store ()

  FileInputStream compareFis = new FileInputStream(compareFile); Properties compareProperties = new Properties(); compareProperties.load(compareFis); .... StringBuilder value=new StringBuilder(); for (Entry<String, SortedSet<String>> entry : resultSet) { SortedSet<String> values = entry.getValue(); for (String string : values) { value.append(string).append("\n"); } } compareProperties.setProperty("test.key",value); FileOutputStream fos = new FileOutputStream(compareFile); compareProperties.store(fos,null); fos.close(); 
+1
source

You need something like this:

 "test.key=Hello\\nWorld!" 

where "\\n" is actually \n .

0
source

Exit \ n before serializing it. If you are going to read what you output the file, your reading code should know about escaping.

0
source

You can also see Apache Commons StringEscapeUtils.escapeJava (String).

0
source

Source: https://habr.com/ru/post/1411144/


All Articles