• Post Reply Bookmark Topic Watch Topic
  • New Topic

seek deals in bytes?  RSS feed

 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seek


pos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.

then why on putting character (= 2 bytes ) with writeChar() in file, it treats it as one byte space ??

or my observation is wrong

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you are talking about either DataOutputStream or RandomAccessFile, your observation is indeed wrong. From the Javadoc pages of these two classes:
Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Writes a char to the file as a two-byte value, high byte first. The write starts at the current position of the file pointer.

As you see, both write to bytes.
 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Assuming you are talking about either DataOutputStream or RandomAccessFile, your observation is indeed wrong. From the Javadoc pages of these two classes:
Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Writes a char to the file as a two-byte value, high byte first. The write starts at the current position of the file pointer.

As you see, both write to bytes.




thanks for response..... please check this code ......





/*

output
a
5689
6.125
5689
true
*/

why 5682 comes in output after "f.seek(2);"
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see 5682; I see 5689, both in what you posted and from my own console when I run that program.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Assuming you are talking about either DataOutputStream or RandomAccessFile, your observation is indeed wrong. From the Javadoc pages of these two classes:
Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Writes a char to the file as a two-byte value, high byte first. The write starts at the current position of the file pointer.

As you see, both write to bytes.

Um, perhaps you meant that both write two bytes? As in the number 2?
 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I don't see 5682; I see 5689, both in what you posted and from my own console when I run that program.



sorry ! typing mistake

so these data are treated as a byte actually ???
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could be wrong, but I think abhay's confusion is all about the old zero-based vs 1-based indexing.

@abhay: The start of the file is at seek(0), not seek(1). The char occupies the first two bytes at index 0 and 1.
 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:I could be wrong, but I think abhay's confusion is all about the old zero-based vs 1-based indexing.

@abhay: The start of the file is at seek(0), not seek(1). The char occupies the first two bytes at index 0 and 1.


thanks, i would like to ask one more thing that all these data items (5689, 6.125, 5689) are treated as 3 bytes only ???
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Rob Spoor wrote:Assuming you are talking about either DataOutputStream or RandomAccessFile, your observation is indeed wrong. From the Javadoc pages of these two classes:
Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Writes a char to the file as a two-byte value, high byte first. The write starts at the current position of the file pointer.

As you see, both write to bytes.

Um, perhaps you meant that both write two bytes? As in the number 2?

Uhm, yes. That happens to me sometimes, when I think too phonetically. It's a result of not being a native English speaker / writer.

abhay jain wrote:
Darryl Burke wrote:I could be wrong, but I think abhay's confusion is all about the old zero-based vs 1-based indexing.

@abhay: The start of the file is at seek(0), not seek(1). The char occupies the first two bytes at index 0 and 1.


thanks, i would like to ask one more thing that all these data items (5689, 6.125, 5689) are treated as 3 bytes only ???

The number of bytes depends on the data type:
- byte requires 1 byte.
- char and short require 2 bytes.
- int and float require 4 bytes.
- long and double require 8 bytes.
- for boolean it's not specified in memory; with RandomAccessFile, DataInputStream and DataOutputStream it's 1 byte (as specified by interfaces DataInput/DataOutput).
 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The number of bytes depends on the data type:
- byte requires 1 byte.
- char and short require 2 bytes.
- int and float require 4 bytes.
- long and double require 8 bytes.
- for boolean it's not specified in memory; with RandomAccessFile, DataInputStream and DataOutputStream it's 1 byte (as specified by interfaces DataInput/DataOutput).



o my god ! this boolean stuff confused me

thanks Rob Spoor thanks a lot to all my friends.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!