aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Files" Watch "Files" New topic
Author

Files

Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
<code>
consider the code below:
import java.io.*;
class filo{
static byte h[];
public static void main(String[] args) throws IOException{
byte b[] = {'J','A','V','A','2'};
FileOutputStream fos = new FileOutputStream("java.txt");
fos.write(b);
fos.close();
FileInputStream fis = new FileInputStream("java.txt");
int i = fis.read();
System.out.println(fis.available()+" and "+i);
}
}
Why does fis.available() print 4 shouldn't it be 5? and how does the fis.read() print 74?
Would appreciate a reply?
Thanks
</code>
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Are you sure this code compiles and runs? The array b is of type byte and you are assigning char values to it, without uding any explicit cast.
The compiler is not going to like it.
Can you post a version of the code that actually compiles and runs?
Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
Hi Shivaji :
The code complies and runs perfectly.
DID u try it ???
Thanks
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
well I tried it and the code does not compile!!!
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
ok
let's see.
as for the first question im no expert but in the documentation it says: available: the number of bytes that can be read from this file input stream without blocking.
maybe it prints 4 and not 5 because of that blocking thing (no idea sorry).
as for the second question the char J is converted to a byte (which is 74 when printed out). when you use the read method you get the first byte i.e 74 and it prints it.
you can convert it (cast) it back to char and then print it and it will give u J.
patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
interesting question.
i've tried the code and notice
that fis.avaialable() will always return
b.length()-1. so 5-1=4. but i dont know why.
as fis.read() will return an integer, only
8 less significant bits of integer is used.
74 is the integer(ascii?) value of J.
Deepti Dixit
Greenhorn

Joined: Dec 27, 2001
Posts: 17
Why does this print JAV2 instead of JAVA2 ?
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Deepti :
I am not sure why the second A is missing from the output. Try the following code,it prints the text back properly.


Ayan :
Of course I tried to compile the code and it fails at the line that the array of chars is assigned to the array of bytes. I am using JDK 1.3.1
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
weird!
i think its a bug actually.
under jdk 1.4.0 BETA it compiles succesfully!
you sure under jdk 1.3.1 it doesnt?
Andrew Cheng
Greenhorn

Joined: Jan 09, 2002
Posts: 12
To Arsho:

In your code,you read one which is i and the left in the stream is now 4.
If you change the code above to,

System.out.println(a + " and " + i);
You should get "5 and 74"
To Deepti:

In your code,the value of "fis.available()" will
be decreased by 1 when you read from the stream and the value is not const.You should change the code to the following,and you can get the right result.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Files