Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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?

 
Master Rancher
Posts: 3276
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
Master Rancher
Posts: 3276
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.
 
Saloon Keeper
Posts: 4794
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What java code were you using to read the file back in?
 
Sheriff
Posts: 5130
138
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
Master Rancher
Posts: 3276
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
Sheriff
Posts: 5130
138
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
Sheriff
Posts: 5130
138
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.
 
Master Rancher
Posts: 3633
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
Sheriff
Posts: 5130
138
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!