aspose file tools
The moose likes Beginning Java and the fly likes Line feed / Carraige return Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Line feed / Carraige return" Watch "Line feed / Carraige return" New topic
Author

Line feed / Carraige return

Frank Daly
Ranch Hand

Joined: Mar 31, 2000
Posts: 139
Hi
i have a StringBuffer object and i want to append a line feed or a carraige return to it. Could someone tell me the code to do this please
thanks in advance
frank
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by Frank Daly:
Hi
i have a StringBuffer object and i want to append a line feed or a carraige return to it. Could someone tell me the code to do this please
thanks in advance
frank

Line feed is char 0x000A and Carriage Return is char 0x000D
to add these to your StringBuffer simply use
append(char) so....
StringBuffer sb = new StringBuffer("Hello");
sb.append(0x000A); // for line feed
sb.append(0x000D); //for carrage return
Now in java, there are two escape characters for these
line feed = '\n'
carriage return '\r'
And so it becomes
sb.append('\n');
sb.append('\r');
if you wish to have both,
sb.append("\n\r");


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
ALl the above is true, but please be careful with this. If you are generating a file to be stored on the system and read by other software it is strongly recommended that you add the appropriate end-of-line characters for the platform you are running on. Unix systems use just a linefeed (\n), Dos and Windows use carriage return land inefeed (\r\n), Others use just a carraige return, or lifefeed then carriage return ...
Luckily Java provides
System.getProperty("line.separator")
which gives you a String containing the appropriate end-of-line sequence, so you can just do:
<pre>
StringBuffer sb = new StringBuffer("Hello");
sb.append(System.getProperty("line.separator"));
</pre>
which will then work on any platform. This is an important part of making your program "100% Pure Java"


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
Frank Daly
Ranch Hand

Joined: Mar 31, 2000
Posts: 139
Many thanks to you both
frank
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Line feed / Carraige return
 
Similar Threads
char range
How to open .lzh file created in UNIX using lha.jar
Escape Sequence \r
0x0D characters randomly entered between Java Client and Visual C++ Server
Encrypted password containing carraige return character not getting saved in database