• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

mark and reset not working in BufferedReader

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

Output
br.markSupported():true
Contents of file
abcddefContents of file
java.io.IOException: Mark invalid
at java.io.BufferedReader.reset(Unknown Source)
at BufferedTest.main(BufferedTest.java:42)

When i invoked mark() i was expecting that file has now been marked @ position 1. And any call to reset() later will re-position the marker which is now at the eof because the contents have been read via readLine() to the postion 1. But this did not happpen and i get Mark invalid exception.
Please help.
Deepak
Preparing for SCJP
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,

I believe you've misunderstood the parameter for the mark() method. It's not the offset at which the mark should be created, but the number of bytes that can be subsequently read while still preserving the mark. It always marks the current read position.

See the javadoc for more info.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After using br.mark(100); it worked.
Even after repeadtedly reading mark() am not understanding the concept.
Let me switch of Enrique and try and understand again.
Will appreciate if someone can explain this.
Thanks
Deepak
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output
br.markSupported():true

Contents of file
ab
Contents of file
abcdefg

The question is commented in code.
Still dont understand the purpose of mark().
Thanks
Deepak
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reset() call need not fail after exceeding the specified number of bytes. All that the API says is that it may fail. In other words, you're guaranteed that it won't fail before exceeding that many bytes, but it can fail at any point afterward. The exact point at which it'll begin to fail is dependent on the exact implementation (typically depending on the internal buffer size).
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m just curious that I dont see the mark() and reset() methods listed in K&B chapter 6. Are they really on the SCJP exam?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they're not on the SCJP exam. Apologies for not making that clear.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am running into the same issue. I have below function. I get this error: "java.io.IOException: Mark invalid".  on bufferedReader.reset();
The strange thing is same code works on one server but fails on another one.
Can some one please tell what may be the issue?

 
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is different between the two systems?
 
Hamid Khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are Linux machine. I am wondering if there is some system settings which is causing this issue on one mchine?
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the java software?
 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HK: welcome to the Ranch
 
Hamid Khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What about the java software?



It was found that above method was being called incorrectly based on a configuration a parameter. So nothing wrong with above method.
Thanks for your help!
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for telling us the full story
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic