In this Java IO Tutorial, we will read line of text from the keyboard input. Here we will chain the IO Stream & readers to achieve the goal of reading a line of text from the console window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package tube.coding.examples.javaadv; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { //Sample 006.1: Create BufferedReader using stream chaining InputStream consoleStream = System.in; InputStreamReader sreader = new InputStreamReader(consoleStream); BufferedReader readerBuf = new BufferedReader(sreader); //Sample 006.2: Read data from the KeyBoard String inputStr = null; try { System.out.println("Type something and hit the enter button: "); inputStr = readerBuf.readLine(); } catch (IOException ex) { } //Sample 006.3: Now output what we read to the console window System.out.println("You typed : \n"); System.out.println(inputStr); } } |
Categories: Java-Tube