• Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 2862
32
  • 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: 2862
32
  • 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.
 
Bartender
Posts: 3864
47
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: 4634
129
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: 2862
32
  • 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: 4634
129
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: 4634
129
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: 3348
38
  • 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: 4634
129
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!