2

I came across the below statement in this tutorial.

while an InputStream returns one byte at a time, meaning a value between 0 and 255 (or -1 if the stream has no more data), the Reader returns a char at a time, meaning a value between 0 and 65535 (or -1 if the stream has no more data).

Below is the example code -

Reader reader = new FileReader("c:\\data\\myfile.txt");

int data = reader.read();
while(data != -1){
    char dataChar = (char) data;
    data = reader.read();
}

The code for InputStream is similar as well. Instead of FileReader & Reader ;FileInputStream and InputStream are used. Reader returns char while InputStream stream returns a byte. In both cases when I test using Strings such as "Hello World" , it reads one character at a time. What are the values that is beyond 255 which I can use to see the difference between these byte based and char based input.

Working Program

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class ReaderStream {

    public static void main(String[] args) {
        String data = "படிகஅமைப்பு";
        InputStream input = new ByteArrayInputStream(data.getBytes());
        Reader inputStream = new InputStreamReader(input);

        try {
            int readData = inputStream.read();
            while(readData != -1) {
                System.out.print((char)readData);
                readData = inputStream.read();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("");

         input = new ByteArrayInputStream(data.getBytes());

        try {
            int readData = input.read();
            while(readData != -1) {
                System.out.print((char)readData);
                readData = input.read();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Output

படிகஅமைப்பு
பà®à®¿à®à®à®®à¯à®ªà¯à®ªà¯
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • `Reader` and `InputStream` have different uses. Please use methods / classes what they are intended for! `Reader` <-> character data, `InputStream` <-> raw/binary data. Feel free to use a hammer on a screw, but don't complain (or ask why it's not working). See the article @nullpointer ... points to. – sruetti Sep 17 '17 at 01:11
  • @sruetti I understand the difference. I just wrote this program to see if inputstream returns value 0 to 255 vs Reader which returns values 0 to 65535. Thanks for your inputs. – Punter Vicky Sep 17 '17 at 01:20

1 Answers1

3

Any Unicode character from the Basic Multilingual Plane that's not in the ASCII set will do. Personally, I'm fond of U+0B87 இ (which is from Tamil), or the biohazard symbol U+2623 ☣. Ensure that the file you've opened is saved in a Unicode encoding (UTF-8 is usually the best option, though Java uses UTF-16 internally).

Darael
  • 331
  • 1
  • 6
  • @PunterVicky While it sounds like you've got the hang of the difference, if not, it may be helpful to try a string that contains both ASCII characters *and* non-ASCII BMP characters. Expected output using InputStream reproduces the ASCII ones just fine, with the non-ASCII ones replaced by the kind of mojibake you saw with the Tamil. – Darael Sep 17 '17 at 01:04
  • yes I've got the hang of it. I will try out your suggestion. Thanks again. – Punter Vicky Sep 17 '17 at 01:09