• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why it is printing from start?

 
naved momin
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

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 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 ??
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java 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: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java 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: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 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: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic