import java.util.Scanner; public class ConsoleInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter your name:"); String name = scanner.next(); System.out.println("Hello, " + name); scanner.close(); } }
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FileInputExample { public static void main(String[] args) throws FileNotFoundException { File file = new File("data.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } }In this code, the Scanner object is created with a File object, representing the file "data.txt". The while loop is used to iterate over each line in the file, and the nextLine() method is used to read in each line as a String. The line is then printed to the console. In conclusion, java.util Scanner is a package library used to parse text data from various sources such as the console or a file. Its methods read in data in various data types such as String, int, float, etc.