- 1). Right-click the Java file you want to use to bring up the scanner controls. Click "Open With," then click the Java editor in the list of programs.
- 2). Add the scanner libraries to the top of the Java code. Adding the library makes all of the functions and properties available to your Java functions. Copy and paste the following line of code to the top of the file:
import java.util.Scanner; - 3). Open the file reader. The file reader reads in the file's content before the scanner functions display. The following code opens a file named "myfile.txt":
FileReader reader = new FileReader("myfile.txt"); - 4). Open the scanner. The following code displays the scanner control:
Scanner scan = new Scanner(fin); - 5). Scan the file's content in the scanner. You must loop through each section of the file to scan the content to the printer. The following code scans the opened document line-by-line until the reader completes the loop:
while (scanner.hasNext()) {
str = scanner.next();
}