• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Writing line returns to file

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using FileWriter or FileOutputStream is there any way to write line returns and/or tab characters?
Thanks
Jay
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for line return, you can use "\n" but there is a better(platform independant) way.
System.getProperty("line.separator")
I'm sure there is a \something for tab as well, but I dont know it offhand.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Java Language Specification:


The character and string escape sequences allow for the representation of some nongraphic characters as well as the single quote, double quote, and backslash
characters in character literals (�3.10.4) and string literals (�3.10.5).
EscapeSequence:
\ b = /* \u0008: backspace BS */
\ t = /* \u0009: horizontal tab HT */
\ n = /* \u000a: linefeed LF */
\ f = /* \u000c: form feed FF */
\ r = /* \u000d: carriage return CR */
\ " = /* \u0022: double quote " */
\ ' = /* \u0027: single quote ' */
\ \ = /* \u005c: backslash \ */



[This message has been edited by Cindy Glass (edited March 07, 2001).]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how you write line returns and tabs.
import java.io.*;
public class WriteFile
{
public static void main( String args [] )
{
try
{
FileWriter writer = new FileWriter("file.txt");
writer.write("Hello\tWorld\n");
writer.write("!!!\t!!!");
writer.flush();
writer.close();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
}
 
Jay X Brown
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all very much.
j.b.
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic