I want to save a text in a text file and I need to save the \n character to delimit the elements in the text. The text is as follow:
“This is a java prog. and It saves text in a .txt file.
You can choose where it should be saved.”
How I can save \n (newline) in a .txt file?
The follow is the code segment I used to save text in text file:
FileOutputStream fout = null;
try {
fout = new FileOutputStream(destinationFile);
} catch (FileNotFoundException e) {
System.out.println("output File not found");
return false;
}
Scanner sc = new Scanner(text);
while (sc.hasNext()) {
fout.write((sc.next().getBytes()));
}
fout.close();
this should do the trick:
this will write the line you entered into the standard input, then write a newline, so that the file will be separated by newlines