Forums Register Login

Files

+Pie Number of slices to send: Send
<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>
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Hi Shivaji :
The code complies and runs perfectly.
DID u try it ???
Thanks
+Pie Number of slices to send: Send
well I tried it and the code does not compile!!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Why does this print JAV2 instead of JAVA2 ?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 949 times.
Similar Threads
how to copy a file from one folder to another folder?
how to read a file data into byte array
available(),skip()
How to correctly use this read() write()
Outputstream the file through http response
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:51:18.