Scanning
Objects of type scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type.Breaking Input into Tokens
By default, a scanner uses white space to separate tokens.
First we have create .txt file or we can use our existing .txt file.
A program that reads the individual words in
.txt
and prints them out, one per line.import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ScanXan {
public static void main(String[] args) {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("D:\\doc.txt")));
while (s.hasNext()) {
System.out.println(s.next());
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ScanXan.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (s != null) {
s.close();
}
}
}
}
Output -