This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Writing an Empty Line in a File....

 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I want to write an empty line in a file. I am trying to save a String variable with the value newLine = "\n". And then trying to write it to the file using the method fileOutputStream.write(newLine.getBytes()). But the thing is i only get a black empty square rather than an empty line.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use an IO Wrapper : PrintWriter.println() or BufferedWriter.newLine() and don't forget to close the streams if you do.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created an object of the PrintWriter class passing the file through the constructor.

PrintWriter printWriter = new PrintWriter(outputFile);

And then when i try to write anything to the file nothing turned up.
printWriter.write(NameRemovalConstants.commentLine1); added nothing to the file.
even
PrintWriter printWriter = new PrintWriter(new FileOutputStream(outputFile,false))

Even this didn't help.


 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have modified the code after a little googling and now i have changed the print writer object to
PrintWriter printWriter = new PrintWriter(new FileWriter(outputFile));

But when i am writing to the file i keep getting the empty blank squares.
printWriter.println(ServiceRemovalConstants.commentLine1);
Only this time not at the end of the line but at the beginning.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic