I write the file line by line from the array, each line of the file in the end is an element of the array

// запись массива Π² Ρ„Π°ΠΉΠ» public static void recordFile(String fileName, String[] currentMassive) { try (FileWriter writer = new FileWriter(fileName, false)) { for (int count = 0; count < currentMassive.length; count++) { writer.write(currentMassive[count]); writer.append('\n'); } writer.flush(); } catch (IOException ex) { System.out.println(ex.getMessage()); } } 

The problem: for example, the array element has the following contents

currentMassive [count] = "1 \ n2 \ n3";

After saving to the file the following result is obtained:

 ... информация count-2 информация count-1 1 2 3 информация count+1 ... 

And I expected to get the result in the form

 ... информация count-2 информация count-1 1*БПБ*2*БПБ*3 информация count+1 ... 

where * ATP * is a newline character

Question: How does / (is it possible) to save a variable of type String, namely an element of an array, into a text file as a single line while preserving the characters of a newline?

Thank.

  • one
    Only if, instead of a newline, you will write two characters to the file: \ and n . - Regent

1 answer 1

If I understand correctly, instead of:

 writer.write(currentMassive[count]); 

Write:

writer.write(currentMassive[count].Replace("\\n",""));

Those. you just need to remove from the line \n that hinders, and write a new line to the file.