Carriage return / line breaks with \ n in lines in Android

I am writing an Android application where I am writing a file to disk with one data value per line. Later, such files can be read in the application, and this simple data format is deserialized back to the array. At the moment, I am defining data / row values ​​in serialization and deserialization code with \n .

How does Android carriage control return and such line breaks? Can I use \n safely in this context?

+8
java android string newline carriage-return
source share
3 answers

Better to use

 String lineSep = System.getProperty("line.separator"); 
+24
source share

Yes, the line separator for Windows is \r\n (CR + LF), on Mac \r and on Linux (Android) \n . Java has a smart reader that returns a string without a separator, and println, which uses the System.getProperty("line.separator") platform setting System.getProperty("line.separator") .

+3
source share

1 - like a tug of people who are talking to me, System.getProperty("line.separator") is your friend.

2 - Since android is based on the linux kernel, most likely the line.separator property is actually \n

3 - when reading or writing your files, use a buffered reader and writer, respectively, their readLine and newLine methods . You should not have problems with input / output of files from / for other platforms.

+2
source share

All Articles