This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes read/write Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "read/write" Watch "read/write" New topic
Author

read/write

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114

Output is 28. What conversion atkes place here. What is teh format it is written into the file and retrieved.
Thanx
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Writing and reading is done in bytes.
so if you start with 500, it's binary rep is
111110100
seek(40) positions the line at 40 where you have 540 as a binary rep
i.e.
1000011100
but when reading bytes, since byte is only 8 bits long, you get 00011100 which gives you 28.
hope that helps
---------------------
Amir


Amir
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Yes that surely helps so if such a ? appears in exam i will have to convert that big number to binary represantation is it? ISnt taht time consuming.
Also if a int 10, byte 10 and long 10 are writtena dn retirved from 3rd psition what will be the result u say?
Please help.
thnx
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Yes, it's always the case of converting them to binary rep. the same with binary operator when given two number is decimal and you convert them to binary. I'm pretty sure the exam won't require you to convert very big number into binary, as it's time consuming. they only ask to see if you know the concept behind the question.
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Deepali,
for reading int, long or byte, you have to use the methods, readInt(), readLong(), or readByte()
in your program at line 10 you can substitute the methods to see what output you get!
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
import java.io.* ;
public class RandFile {
public static void main (String [ ] atfd) throws IOException {
FileOutputStream fos = new FileOutputStream ("file") ;
for ( int i = 10 ; i<20 ; i++) {
fos.write(i) ;
}
fos.close() ;
RandomAccessFile raf = new RandomAccessFile("file","r") ;
raf.seek(1) ;
System.out.println (raf.readInt());
System.out.println (raf.read()) ;
raf.close() ;
}
}
This now gives first o/p as 185339150 and second as 15. i dont understand this behaviour at all.
Any documentation link u have got to make this clear to me??
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Ok, The problem comes when you read an int!!!
remember that an int is 32 bits long, so you skip 32 bits for the first file pointer, then you start reading a byte, which must give 00001111 so you see the result as 15!
ofcourse, you must first write atleast 4 bytes
each 8 bits long, before you can read an int! provided now that your pointer starts at 0 so it can read 32 bits. now eachtime you write a byte, you append it to the last byte, so for example
first you write 10 which is 00001010 then you write 11 which is 00001011. in total you now have
0000101000001011 and now you can imagine that when you write upto 20, and then position the pointer at 1 after reading 32 bits(for readInt) then you get the huge number first.
I hope that clears your doubt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10

Originally posted by Deepali Pate:
Output is 28. What conversion takes place here. What is the format it is written into the file and retrieved.


fos writes bytes, the last 8 bits of "540" are 00011100 which is "28" in decimal.

I've changed "file" to "file.txt" here so you can open it in a text editor and look at it.

Also, if you want to start at the beginning of the file when you use raf.seek(), either comment out the line or use raf.seek(0).

You won't get a question this complex on the exam.

(edited)
By the way, when you look at the file, you won't see "28", you'll see the ascii characters represented by those numbers. For instance, if you raf.seek(80), you'll get an output of "68" which in your text file is "D". (580 in binary is 1001000100. fos writes the last 8 bits "001000100" which is "68" in decimal)
[ July 05, 2002: Message edited by: Marilyn deQueiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
just to clarify it more..
10 ---> 00001010
11----> 00001011
12----> 00001100
13----> 00001101
14----> 00001110
15----> 00001111
first you position the file pointer at 1, which is after number 10, so it starts from 11. then you read an int (32 bits) from readInt() which positions the file pointer at after 14. then you read a byte from read() and ofcourse you get 15.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
hi folks! I'm new to this forum and am learning alot by studying the questions and responses here. Going to take my Prog exam on July 22 ...
To: Deepali Pate


from the C:\jdk1.3.1\docs\api\index.html (comes with JDK and you can install documentation on your computer or else go to Sun web site online docs)
public abstract class OutputStream
public abstract void write(int b)
throws IOException
"Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."
so ...
when you go to read this file with
"System.out.println (raf.readInt());"
you'll find that the documentation for the RandomAccessFile shows:
"Reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:
(b1 << 24) | (b2 << 16) (b3 << 8) b4

This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.
Returns: the next four bytes of this file, interpreted as an int.
so ... You originally "wrote" 10 ints to the file, each stored into 1 byte using the write method of the FileOutputStream Class however, you are reading 4 bytes there instead of 1 byte to try to get at the numbers you stored.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
To: Deepali Pate
try this code:

when you run it you'll get this:
C:\jdkPrograms\fileSystemStuff>java RandFile
10
11
12
13
14
15
16
17
18
19
168496141
235868177
Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readInt(RandomAccessFile.java:607)
at RandFile.main(RandFile.java:23)
what happened you ask?
you stored 10 bytes, ech containing an int (within the range 0-255)
then you read one byte at a time in the first read call,
then you read 4 bytes at a time, and on the third read attempt, you ran out of bytes to read thereby throwing the (unhandled !) exception.
hope this helps!
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Another useful method you can use in these situations is the toBinaryString method defined in the Integer class
so for example int i = 10;
System.out.println(Integer.toBinaryString(i));
would print 1010.
 
GeeCON Prague 2014
 
subject: read/write