| Author |
How to use the string formatter method
|
Guy Rich
Ranch Hand
Joined: May 03, 2011
Posts: 33
|
|
Hi all, does anyone know of a good online resource that simply and definitevly explains how to use the string formatter method...?
I need to write a series of "records" into a set ascii text files. I need to "delimit" each "record" with a cr-lf sequence in a windows 2008 server environment.
Therefore I'm trying to figure out how to add a \r\n character string at the end of each "record". I tried a "record_string.append(CR) and LF" ; but it didn't work.
Thanks much
Guy
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
\r\n should definitely work. Usually you should use %n for the system-specific new line string, but as you explicitly require a Windows line break \r\n is good. Could you show us your formatting code?
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Anton Shaykin
Ranch Hand
Joined: Dec 13, 2009
Posts: 57
|
|
|
Also, to encourage Java cross-platform paradigm, you should better use System.getProperty("line.separator") which will return "\r\n" for Windows and "\n" for Linux.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
Hello Guy, I noticed that you asked the same question on StackOverflow, where I also posted an answer. As Rob says, using \r\n should work, and you can find a list of escape sequences in section 3.10.6 of the Java Language Specification.
When you're using for example a StringBuilder to build a record, you can just append \r\n to it:
Please BeForthrightWhenCrossPostingToOtherSites.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
Start in the Java™ Tutorials, and look at the sections marked "formatting" or similar. Look up the java.util.Formatter class, and the String#format method takes the same arguments as System.out.printf and Formatter#format.
|
 |
 |
|
|
subject: How to use the string formatter method
|
|
|