• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

PrintWriter prints new line (blank) at the end of the text file  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a function that writes random integers to a text file. The problem is that it adds an extra line at the end of the file. I am trying to figure out why this is happening. Any ideas or suggestions?

 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you viewing the file to see what is in it?  Can you look at its contents in a hex editor to see its contents?

The code works as expected for me. No extra blank lines at the end of the file.
 
Nataly Carbonell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using textedit. I also tried textmate and it still shows the newline at the end. I was able to confirm that there was indeed a line at the end while trying to read the file. Java gave me an exception because it wasn't able to read an integer in that last blank line.

Screen-Shot-2018-02-25-at-3.04.46-PM.png
[Thumbnail for Screen-Shot-2018-02-25-at-3.04.46-PM.png]
last blank line appears in both textmate and textedit
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you look at the file with a hex editor to see what is in it?

Is there any other code that could write to the file after the posted code writes to it?

it wasn't able to read an integer


The posted code writes Strings, not integers to the file.
 
Marshal
Posts: 5634
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the OP is reading the file back in with Java, I think they are looking at it with a text editor.
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java gave me an exception because it wasn't able to read an integer in that last blank line.

 
Knute Snortum
Marshal
Posts: 5634
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this complete program:
...I also get an "extra line" at the end, on Windows 10, using Notepad++.  But if I view all characters, I can see that there's actually nothing on that line.  That is, this is an artifact of the way Notepad++ displays a text file.  See attachment.
blank_line.png
[Thumbnail for blank_line.png]
 
Knute Snortum
Marshal
Posts: 5634
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Java gave me an exception because it wasn't able to read an integer in that last blank line.


Ah, sorry.
 
Rancher
Posts: 3797
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Every number has a newline character after it.
That's what println does.

If you don't want a newline then the last number has to be output using just print.
 
Knute Snortum
Marshal
Posts: 5634
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java gave me an exception because it wasn't able to read an integer in that last blank line.  


I wrote two short programs, one using a BufferedReader and one using Scanner and both work fine.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!