This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

why it is printing from start?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose file "a" has following content


and file "b "is empty as if for now .

after i run the code what should print is


what it is printing is



i suppose what i have said my code to read is

 
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should reread the Javadocs for java.io.Reader The 2 in this line: is the offset into the character array, not the point you start reading the file. In other words, it's where you'll start storing the characters you read from the file into buffer. After that read, you'll have something like:

[nil,nil,'h','e','y',' ','h','o','w',...]

in the array, and when you go to write, also starting from offset 2, you'll start at the first 'h'.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Charles wrote:You should reread the Javadocs for java.io.Reader The 2 in this line: is the offset into the character array, not the point you start reading the file. In other words, it's where you'll start storing the characters you read from the file into buffer. After that read, you'll have something like:

[nil,nil,'h','e','y',' ','h','o','w',...]

in the array, and when you go to write, also starting from offset 2, you'll start at the first 'h'.


so you are saying that there will be two white spaces before hey ??
 
Sheriff
Posts: 21842
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not white spaces, NULL chars*, with value 0 or '\0'. In C these are used to mark the end of Strings.

* NULL, not null. null chars do not exist, as null is only possible for object references.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Not white spaces, NULL chars*, with value 0 or '\0'. In C these are used to mark the end of Strings.

* NULL, not null. null chars do not exist, as null is only possible for object references.


so according to you it should be null character , so null means there is nothing associated with first two char but it is showing me the first two char also
like you
when there is 2 null
it should should me null ,null , 'u' right ?
but it is showing me 'y' 'o' 'u' wholly
 
Greg Charles
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, no. I don't think this has clicked for you yet.

Let's imagine that your chars are cars, and buffer is a row of parking spaces. You tell your H-car to go park, not in the first space, but in the third. (That's what the offset of 2 is doing for you.) The following cars, the e-car, y-car, etc., then park next to the H-car, which is the fourth space, then the fifth space, etc. That way you get all the cars parked, but the first two parking spaces are empty. That's what your buffer looks like after the read. Now, for the write. You tell someone to go the parking space and get a car. Does he go to the first space and get confused because there's no car there? No he doesn't, because you used an offset of 2 in your write as well, so you're telling him to go to the third parking space, where he'll find that H-car waiting for him.

Now to make up for confusing you with an analogy, I'll just straight tell you: you want a 0 offset on the read, and a 2 offset on the write. I hope you'll be able to visualize why that works.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Charles wrote:Hmm, no. I don't think this has clicked for you yet.

Let's imagine that your chars are cars, and buffer is a row of parking spaces. You tell your H-car to go park, not in the first space, but in the third. (That's what the offset of 2 is doing for you.) The following cars, the e-car, y-car, etc., then park next to the H-car, which is the fourth space, then the fifth space, etc. That way you get all the cars parked, but the first two parking spaces are empty. That's what your buffer looks like after the read. Now, for the write. You tell someone to go the parking space and get a car. Does he go to the first space and get confused because there's no car there? No he doesn't, because you used an offset of 2 in your write as well, so you're telling him to go to the third parking space, where he'll find that H-car waiting for him.

Now to make up for confusing you with an analogy, I'll just straight tell you: you want a 0 offset on the read, and a 2 offset on the write. I hope you'll be able to visualize why that works.


yes , i got you now ,
but after changing the logic to

i assume that mine first two cars will be neglected and writer will take up the 3rd car right ?
but output is
 
Rob Spoor
Sheriff
Posts: 21842
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first three "cars" are not "how" but "hey".
 
Greg Charles
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not the third car (char), but the fourth. Remember in Java, we start counting from 0. Another way of looking at it is that 2 means to skip the first 2 cars, while 3 means to skip the first 3. One more nitpick, "reads" is how many chars you read. If you write them, but skip the first two, will you still write the same number of chars?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got that thanks a lot
 
Greg Charles
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Let's get him boys! We'll make him read this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!