• 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

Filewriter and Unwanted Blank Lines

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my first question on coderanch. I have done some googling ( and the results did include coderanch)
but the problem there is that the questions and answers I found are sufficiently different from my own to make them effectively useless to me.
So here is where I am :

I have an existing text file and I want to add/append a new record to it.
The code below does indeed do that but for some reason it also adds a blank line, which I absolutely do NOT want.
How do I prevent the blank line being added, please ?
Thank you in advance.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, welcome to the ranch.

As for your newlines, you have two. First, the string printed has a newline. And second, the println() method adds a newline. Is that the blank line that you are referring to?

Henry
 
Frederick Douglass
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As for your newlines, you have two. First, the string printed has a newline. And second, the println() method adds a newline



I must admit confusion here. Yes, there are two references to newline and the println adds a newline.
But, if I understand you correctly, you are suggesting that the println is adding the blank line.
In which case how is the ( non-blank ) newline being added ?

And thank you for your rapid reply.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two new lines in a row will create a blank line, whether the NL comes directly from the String ("\n") or from println();
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Frederick Douglass wrote:... you are suggesting that the println is adding the blank line.
In which case how is the ( non-blank ) newline being added ?


Maybe you don't realize the differnece between print() and println(). With print() your string will be output to the file, but with println(), in addition to outputing your string to the file, a new-line sequence will also output to the file (\n for Linux/Mac, \r\n for Windows).
 
Frederick Douglass
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Removing the "ln" from "println" did work, thanks.
I did know the significance of "ln" in the context of "System.out.println".
But here I assumed the "\n" was about positioning of the line throw rather than the line throw itself.

Another problem solved, thanks very much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic