import java.util.Scanner; public class ReadInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter any text: "); String inputText = scanner.nextLine(); System.out.println("You entered: " + inputText); scanner.close(); } }
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadFromFile { public static void main(String[] args) { try { File file = new File("input.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); } } }Here, we create a `Scanner` object that takes input from the `input.txt` file using `File` class. We then read the file line by line using `nextLine()` method inside a loop and print each line using `System.out.println()` method. The package library for `java.util.Scanner` is `java.util`.