Escape sequences are handled by the Java compiler, and therefore they must appear in the Java source code. The compiler will see, for example, a '\' followed by a 'n', and replace them both with a single newline character.
If the characters '\' and 'n' are read at runtime from a file or from the command line, they're just two separate ordinary characters.
To reiterate, escape sequences have no meaning at all to Java when a program is running; they only mean something to the compiler when they appear in source code.
Embedding special characters into the arguments to a Java program is actually not a Java issue at all -- it's an operating system issue. If your OS's command line supports a way to type those characters, then you can do it, but if it doesn't, then you're out of luck. On Linux, under most shells, you can simply type
java MyProgram 'line1 line2 line3'
and Java will get the three separate lines of input as a single argument with embedded newlines. I have no idea if anything similar is possible on Windows.
If you do need to treat \n in a command-line argument as meaning new-line, then you could process each command-line argument to do so.
You would use String.replaceAll("\\n", System.getProperty("line.separator")), I think. The double-backslash here means "really a backslash". It is better to use the system property, rather than "\n", because there are, at least theoretically, platforms where "\n" is not the right new-line sequence to use. [ November 23, 2004: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
In the first two cases, a String literal is processes by the Java compiler and all occurences of \n are replaced by the unicode character '\u000A' early in the compilation process.
In the last case, the command line arguments are processed by the operating system. The handling of eacape sequences is specific to the OS and command shell you are using. In your case, \n is not treated specially in the command line by your OS. However, you can process these arguments any way you wish with StringTokenizer.
SCJP 1.4, SCWCD in process
Joined: Aug 18, 2004
Ernest - thanks for your explaination, I now understand why it does not work.
Peter - thanks for your solution, which I like the sound of; however, I'm having trouble getting it to work. So, the String.replaceAll method replaces all occurances of the first string with the 2nd. No problem. I can get this to work with any string combo passed as the first string - any that is without a \ !!! I've tried all sorts to try to fool it, but it just wont find and replace \n (but as mentioned, it will do just n or any other string) To try to fool it I've tried all sorts as the first parameter: "\n" (I know the compiler will replace this as a real cr so wont work, but worth a try anyway !) "\\n" "\\"+"n" defining a string with the value "\\n" or "\\"+"n" and passing the reference of the string to the method
Nothing works !
Any ideas ?
And the StringTokenizer suggestion ?? Yes, I can see how that may work (or use String.split), but will it suffer in the same way as replaceAll in that it wont recognise the \ properly ?? And of course, it will make my overall code very messy as I would then need to deal with an array rather than a string
Joined: Aug 18, 2004
OK, I've found the answer (or at least one answer) and its messy as you like !
As mentioned in my previous posting, I can not pass \ or \\ to the replaceAll method no matter how I try. So, I got a bit creative ....
Basically, I create an empty StringBuffer and use the append method 3 times to append 2 \'s and the n. I then pass the StringBuffer to the replaceAll method with the toString() method !!!