Search code examples
javaandroidftpapache-commons-net

Apache Commons Net - System.getProperty("line.separator") - does not work in Android


I use the Apache Commons API to append a new line to a file using the FTPClient class. When I run the following code in Java, a new line is appended to the file on the FTP server. However, when I run the same code in Android, the String is appended to the file without a new line.

Why is the new line using - System.getProperty("line.separator") - not transferred via FTP under Android?

Also, the new line is correctly displayed in the LogCat but does not work in the txt file on the FTP server. Maybe there is a difference in character encoding between Java and Android?

Thank you very much.

String log = System.getProperty("line.separator") + "blablabla";        
boolean done = ftpClient.appendFile("log.txt", new ByteArrayInputStream(log.getBytes("UTF-8")));

System.out.println("LOG: " + log);

Solution

  • As the file is on the server, I wouldn't think you'd want the client's value of System.getProperty("line.separator"); You want to know what the line separator on the server is, especially as you're working (apparently) in binary mode and so the FTP middle layer can't do line-ending conversions for you. (Which was once — possibly still is — quite a common thing for FTP clients and servers to do; it was called "ASCII" mode. [Ah, those halcyon days, when we thought we could assume text would be in ASCII, despite knowing, deep down, that that just wasn't sustainable... Like two-digit years...])

    You could either query that information from the server, or choose to always use a particular line separator in your server-side log file. If the latter, \n would be a good choice if you're using a *nix-based server. If you're always on the Microsoft stack on the server, then \r\n would probably be a better choice.