<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>
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?
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.
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.
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.
Post by:autobot
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards