This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes writeX() method.. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "writeX() method.." Watch "writeX() method.." New topic
Author

writeX() method..

Angela Xia
Greenhorn

Joined: Jan 03, 2002
Posts: 10
Here is the question from JQPlus
How many bytes will be written by the following code snippet in the file?
...
DataOutputStream dos=new DataOutputStream( new FileOutputStream("data"));
int x=0x12341234;
dos.writeByte(x);
dos.writeChar(x);
dos.writeShort(x);
dos.writeInt(x);
dos.close();
the answer is 9. why? Thanks!


Angela
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
this one isnt as hard as u might think:
dos.writeByte(x); //writes a byte
dos.writeChar(x); // writes 2 bytes
dos.writeShort(x); // writes 2 bytes
dos.writeInt(x); // writes 4 bytes

alltogether 9.
first you write a byte to the file.
then you write a char and a short (each is 16 bit meaning 2 bytes).
then an int ( 32 bits - 4 bytes).
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: writeX() method..
 
Similar Threads
What to use regarding streams
URGENT! How to write an Object to a Socket
Drowning need someone to throw me a line.
How to create a counter in my page?
Writing to a file.