Carriage returns/line breaks with \\n in strings i

2019-01-17 23:45发布

问题:

I'm writing an Android app where I'm writing a file to disk with one data value per line. Later, such files can be read back into the app, and this simple data format is deserialized back into an array. At the moment, I'm delineating data values/lines in the serialization and deserialization code with \n.

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

回答1:

Its better to use

String lineSep = System.getProperty("line.separator");


回答2:

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



回答3:

1 - like the tow people before me i say, System.getProperty("line.separator") is your friend.

2 - As android is based on a linux kernel it is most likely that the line.separator property is in fact \n

3 - when reading or writing your files use buffered reader and writer an respectively their methods readLine and newLine. You should not have any trouble dealing with input/output files from/for other platforms.