• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use the string formatter method

 
Guy Rich
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20609
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\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?
 
Anton Shaikin
Ranch Hand
Posts: 65
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic