• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

printing new line in file

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys

i want to print a new line in a file whenever i need

this is my code

File f= new File("sqlscript.sql");
f.createNewFile();
FileOutputStream fos=new FileOutputStream(f);
String s= new String("script file for inserting \n records);

byte[] b= s.getBytes();
fos.write(b);

i want to print this "script file for inserting" in first line

and second line records

looking for your replies
regards
amir
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're writing a text file, you should generally use Readers and Writers to work with it, not Streams. If you wrap an OutputStreamWriter around the FileOutputStream, and then a BufferedWriter around the OutputStreamWriter, you can use the BufferedWriter.newLine method to start a new line in the file.

Note that the sequence of characters written by newLine is OS-dependent - it writes something different on Unix than on Windows.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you can check the line separator that way :
System.getProperty("line.separator")
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i will be very much thankful to you

all if any body sends me a sample code

iam also working on that

regards
amir
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sample code for what - wrapping a Writer around a Stream? That's just one line of code; it should not hard be hard to figure out. What do you have so far?
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes ulf i got it

File f= new File("sqlscript.sql");
f.createNewFile();
FileWriter fr= new FileWriter(f);
BufferedWriter outputB= new BufferedWriter(fr);

outputB.write("insert into table ");
outputB.newLine();
outputB.write("insert into table ");
outputB.close();

regards
amir
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic