non blocking bufferedreader

Solutions on MaxInterview for non blocking bufferedreader by the best coders in the world

showing results for - "non blocking bufferedreader"
Kerry
01 Nov 2020
1import java.io.BufferedReader;
2
3public class nonblockingIO {
4  
5  public void read() {
6    
7    // Initialising BufferedReader object
8    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9    
10    // This can be replaced for a conditional loop
11    while (true) {
12      
13      /* This is the crucial line.
14        This line checks if there is any data in the buffered BufferedReader.
15        If there isn't any data, the code inside the if statement is skipped
16        and the buffered reader is thus non-blocking as the program does not
17        wait for a user input (i.e. the user to press enter) */
18      if (br.ready()) {
19        
20        // Reading data from the buffered reader
21        String output = br.readLine();
22
23      }
24
25      // Other code here...
26
27    }
28    
29  }
30  
31}