File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use the string formatter method" Watch "How to use the string formatter method" New topic
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: 19541
    
  16

\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 - OCEEJBD 6
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: 13868
    
  10

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: 36475
    
  16
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.
 
Don't get me started about those stupid light bulbs.
 
subject: How to use the string formatter method
 
Similar Threads
Record length desription
Help! Problems with a method calling itself...
File I/O problem: Java program inserts ^M into text file
readLine()
Write CR/LF at the end of a line