0

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);
  • Have you looked at what you get back from `System.getProperty("line.separator")` in your two scenarios? That would pretty much be the first thing to do... – T.J. Crowder Jun 23 '14 at 18:37
  • In Java, I get back a String with length 2, and a hash code of 413. – user2175056 Jun 24 '14 at 08:07
  • In Android, I get back a String with length 1, and a hash code of 10. – user2175056 Jun 24 '14 at 08:08
  • Can I figure out which line separators were used from this?? – user2175056 Jun 24 '14 at 08:15
  • @ user: Why in heaven's name are you quoting lengths and hashcodes, rather than the actual characters? But I can tell you with certainty that the one with length=2 is `"\r\n"` (you must be doing that on Windows) and the one with length=1 is `"\n"`. (Lurkers: Yes, I'm so pedantic, I *did* double-check the hashcodes matched.) – T.J. Crowder Jun 24 '14 at 08:53
  • I tried printing the characters to the console and all I saw were new lines... So I quoted the hash codes... – user2175056 Jun 24 '14 at 09:06

2 Answers2

0

Windows uses \r\n as its line separator, unlike UNIX which uses just \n So,try to use

String log = "\n" + "blablabla"; 

or

String log = "\r\n" + "blablabla"; 

instead of

String log = System.getProperty("line.separator") + "blablabla"; 
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875