try { BufferedReader reader = new BufferedReader(new FileReader("file.txt")); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); } reader.close(); } catch (IOException e) { e.printStackTrace(); }
try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your name: "); String name = reader.readLine(); System.out.println("Hello, " + name + "!"); } catch (IOException e) { e.printStackTrace(); }In this example, we create a new BufferedReader object to read input from the keyboard via the System.in input stream. We prompt the user to enter their name, read their input with readLine(), and then output a personalized greeting. In both examples, we are using the java.io package library.