my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes why it is printing from start? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "why it is printing from start?" Watch "why it is printing from start?" New topic
Author

why it is printing from start?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

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



The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

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

Joined: Jul 03, 2011
Posts: 688

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

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

Joined: Oct 01, 2001
Posts: 2841
    
  11

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

Joined: Jul 03, 2011
Posts: 688

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

The first three "cars" are not "how" but "hey".
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

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

Joined: Jul 03, 2011
Posts: 688

i got that thanks a lot
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why it is printing from start?
 
Similar Threads
readLine() and read() functionality
replacing content in a file
fileREader and FileWriter
how jvm understand that from which point print in newline while coping from file a to b ?
what is wrong with my code?.