Skip to main content
Formatting improved
Source Link
Jagger
  • 10.5k
  • 11
  • 60
  • 100

its giving anThe following code is giving a compilation error of storing charchar into bytebyte. howHow do I store the bytes read by the fin.read()fin.read() function into a byte array and print it.?

    import java.io.*;FileInputStream;

class IO {
    public static void main(String args[]) {
        int i;
        int j=0;j = 0;
        FileInputStream fin = new FileInputStream("file.txt");
        byte[] b = new byte[100];
        do {
            i=i = fin.read();
            j++;
            b[j] = (char) i;
        } while( (i != -1);
        System.out.println(b);
    }
 
}

Output:

possible loss of precision
found   : char
required: byte
            b[j] =(char) i;
                  ^
1 error

How do I make it work? readRead the file in to byte array and then display?

its giving an error of storing char into byte. how do I store the bytes read by the fin.read() function into byte array and print it.

    import java.io.*;
class IO{
    public static void main(String args[]){
        int i;
        int j=0;
        FileInputStream fin = new FileInputStream("file.txt");
        byte[] b = new byte[100];
        do{
            i= fin.read();
            j++;
            b[j] =(char) i;
        }while( i != -1);
        System.out.println(b);
    }
 
}

Output:

possible loss of precision
found   : char
required: byte
            b[j] =(char) i;
                  ^
1 error

How do I make it work? read the file in to byte array and then display?

The following code is giving a compilation error of storing char into byte. How do store the bytes read by the fin.read() function into a byte array and print it?

import java.io.FileInputStream;

class IO {
    public static void main(String args[]) {
        int i;
        int j = 0;
        FileInputStream fin = new FileInputStream("file.txt");
        byte[] b = new byte[100];
        do {
            i = fin.read();
            j++;
            b[j] = (char) i;
        } while (i != -1);
        System.out.println(b);
    }
}

Output:

possible loss of precision
found   : char
required: byte
            b[j] =(char) i;
                  ^
1 error

How do I make it work? Read the file in to byte array and then display?

Source Link
Mahin Khan
  • 776
  • 2
  • 11
  • 19

Using a byte array

its giving an error of storing char into byte. how do I store the bytes read by the fin.read() function into byte array and print it.

    import java.io.*;
class IO{
    public static void main(String args[]){
        int i;
        int j=0;
        FileInputStream fin = new FileInputStream("file.txt");
        byte[] b = new byte[100];
        do{
            i= fin.read();
            j++;
            b[j] =(char) i;
        }while( i != -1);
        System.out.println(b);
    }

}

Output:

possible loss of precision
found   : char
required: byte
            b[j] =(char) i;
                  ^
1 error

How do I make it work? read the file in to byte array and then display?