Read and print a byte with read and write

0

I have this code:

public static void main(String[] args) {
        try {
            int n = System.in.read();
            System.out.write(n);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

If I enter, for example, an 'a', the result is that it does not print anything. Why can not I print the byte with write?

Thanks in advance.

Greetings

    
asked by pelaitas 25.04.2018 в 09:12
source

2 answers

2

The problem is that the object System.out is of type PrintStream which uses in its implementation an instance of BufferedOutputStream to perform the scripts.

The BufferedOutputStream do not perform a physical write each time you invoke any of its methods write() , but, it will store the data that you have been sent to write until an internal buffer is filled and it is then when the physical writing of the data. The objective of this mechanism is to optimize the processes of physical writes (for example in a hard disk) since these are very expensive. If we write the data byte by byte, our program takes longer to write the data itself than in its internal functioning.

There is a way to force physical writes, and it is using the flush() method, which forces you to write the contents of the internal buffer.

If you read the documentation for the method System.out.write(int) you can see that only the flush() method will be called if the character that represents the parameter is a line break

  

If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

For all the above is that the character is not written in the console, because it remains in the internal buffer of the object System.out and upon completion of the execution of the program this data is lost.

To correct the problem, just add a flame to flush()

    try {
        int n = System.in.read();
        System.out.write(n);
        System.out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

or print a line break

    try {
        int n = System.in.read();
        System.out.write(n);
        System.out.println();
    } catch (IOException e) {
        e.printStackTrace();
    }

or, use the print() or println() method instead of write() . That if, to use any of these you must cast your whole to char

    try {
        int n = System.in.read();
        System.out.print((char) n);
    } catch (IOException e) {
        e.printStackTrace();
    }

I hope you have explained as clearly as possible and that you have understood it.

    
answered by 25.04.2018 в 10:17
0

It is normal that when you pass an 'a' do not print anything, why? Let's analyze your code a bit.

To pick up a value by keyboard, use the following line:

int n = System.in.read();

If you notice, what you collect is assigned to an integer, which is normal when entering an error.

When giving an error and having a try catch, what it does is enter the catch (skip the line of System.out.write (n)). For you to check that it is like this, you can put in the catch, for example,

System.out.write("X");

Then, when entering a letter, you will paint the X on the screen.

To avoid this, what you can do is, cast to char the collected by keyboard and assign it to a char variable (suggested by @ lois6b).

To do it, it would be done as follows:

char carRecogido = (char)System.in.read();
System.out.write(carRecogido);
    
answered by 25.04.2018 в 09:19