File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to use the string formatter method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use the string formatter method" Watch "How to use the string formatter method" New topic

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

Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

\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?

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

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 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
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.
I agree. Here's the link:
subject: How to use the string formatter method
It's not a secret anymore!