0
public static void main(String[] args) throws IOException {
       FileReader in = new FileReader("C:\\Users\\itmaint\\eclipse-workspace\\IO\\src\\IO\\file1.txt");
        int times=0;
        int c;
        while ((c = in.read()) != -1) {
            System.out.println("Reading using Reader");
            times++;
            System.out.println("'"+(char)c+"'"+" : "+c+" ");
            }
        System.out.println("times ="+times);
        in.close();
        times=0;
        FileInputStream in1= new FileInputStream("C:\\Users\\itmaint\\eclipse-workspace\\IO\\src\\IO\\file1.txt");
        while((c=in1.read())!=-1)// reads a byte of data
        {
            System.out.println("Reading using Stream");
            times++;
            System.out.println("'"+(char)c+"'"+" : "+c+" ");
        }
        System.out.println("times ="+times);

        in1.close();
 } 

Output:

Reading using Reader
'H' : 72 
Reading using Reader
'I' : 73 
times =2
Reading using Stream
'H' : 72 
Reading using Stream
'I' : 73 
times =2

The file had the characters "HI". If Reader reads a character why does it have to casted into a char just like Stream , when printing using the System.out.println()?

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
Shiva Shankari
  • 17
  • 1
  • 1
  • 7

0 Answers0