• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Files

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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>
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shivaji :
The code complies and runs perfectly.
DID u try it ???
Thanks
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I tried it and the code does not compile!!!
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this print JAV2 instead of JAVA2 ?
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic