• 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

Question about Char/Byte Streams

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this text file- xanadu.txt

In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.

I can use this Char Stream program to copy it to thatone.txt:



Now, if I want the variable x or the life.read() value printed, something goes wrong. I get this output in thatone.txt

I aauddKbaKa
ttl lauedm ere
hr lh h ardrvr a
hog aen esrls omnDw oasnessa

What I am wondering is why printing the values for x and life.read() seem to change them?

In addition, if I don't use what appears to be the redundant variable x on lines 9-10 I have this code:



That yields this result:



nXnd i ul hnAsaeypesr-oedce:WeeAp,tesce ie,rnTruhcvrsmaueest a
ont uls e.

I am just curious why these things should be important.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read() returns the next character/byte and then skips to the next one. That's why several characters are missing when you call read() more than once in the loop.
 
reply
    Bookmark Topic Watch Topic
  • New Topic