Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

My new lines don't show up in the file but they do show up on the console  RSS feed

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have the following code.



The console's output is:
#
##
###
but the temp.txt file has:
a
#
a
##
a
###

Why don't the a's show up on the console? I put them in as part of the debugging process and now I don't understand why they don't show up in both places. Thanks!
 
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you viewing the file? Can you open it is a hex dumper/editor and see what the file contents actually is?

Also, are your example outputs reversed? It seems like they do not match with the topic heading.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried running this… which is your code with the indentation tidied up and the empty catch got rid of and a different class name. This was on Eclipse Luna on Ubuntu with Java8u45 from Oracle. I got this output
a#
a##
a###
What operating system are you using? Are you on Windows®? It may be to do with the line separator having two characters. On Windows® it is \r\n and on Linux/Unix \n. Older Macs used \r but you will hardly see one of them outside a museum nowadays.
If you are inserting the has one position from the end of your string builders, you are probably inserting it after the \r and before the \n. So you get a\r#\n and the behaviour of the \r and \n vary depending where they are used.
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in Windows 7 and my output from that program was:

a
#
a
##
a
###


And Notepad++ showed the same thing.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what is contained in the file. Depending on the environment, the CR character may cause the printing/display location to be reset back to the beginning of the line, causing previously written characters to be over-written.

     cat temp.txt | od -a
     0000000 a cr # nl a cr # # nl a cr # # # nl
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed lines 30-31 to read thus:… and got this output from Eclipse:
a
#
a
##
a
###
Sounds a good idea to inspect your file with a hex editor, remembering that \n = 0x000a and \r = 0x000d. If the file is encoded in ISO8859-1 or UTF-8 you will probably see them as 0a and 0d.

Depending on the behaviour of the app you are using, obviously in some instances \r causes output to move onto the next line and in others it appears to move the cursor to the start of the current line and the original output is overwritten. Try "aaaa" instead of "a" and see whether you get #aaa.

Shows what happens if you don't stick to your platform‑specific line separator and spit it by inserting things.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see others have already inspected the file for hex characters.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Sounds a good idea to inspect your file with a hex editor

Notepad++ with the HEX-Editor plugin is great for this.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:How are you viewing the file? Can you open it is a hex dumper/editor and see what the file contents actually is?

Also, are your example outputs reversed? It seems like they do not match with the topic heading.


Good point about the topic. The new lines show up in WordPad but not NotePad which is what I used to open the file originally so I should have changed the topic heading when I found out the problem was with NotePad.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have known NotePad handle line ends differently from some other apps.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:This is what is contained in the file. Depending on the environment, the CR character may cause the printing/display location to be reset back to the beginning of the line, causing previously written characters to be over-written.

     cat temp.txt | od -a
     0000000 a cr # nl a cr # # nl a cr # # # nl


I didn't realize the end of line separator was two characters which was what was causing the problem. Thanks for all of the help from everyone!
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!