Sunday, December 11, 2016

How to read a file token to token?

/*Description: this program reads a file and displays it into tokens.This program uses Scanner
class to do the job. There is no comment and explanation. If you know how to read a file then this should be obvious. This is not a good programming habit btw. We must explain or comment on the code. 
 * @author: blankbug
 * 
*/


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class StringTokenizer{
public static void main(String[]args)throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
System.out.printf("Please enter the name of the file to read.\n");
File nameOfTheFile = new File(scan.nextLine());
Scanner fileReader = new Scanner(nameOfTheFile);
String fileContent ="";
while(fileReader.hasNextLine()){
fileContent = fileReader.nextLine();
Scanner scan1 = new Scanner(fileContent);
String scan2 ="";
while(scan1.hasNext()){
scan2 = scan1.next();
System.out.println(scan2);
}//end of inner while loop
scan1.close();
}//end of outer while loop
fileReader.close();
scan.close();
}//end of the main method
}//end of the class

No comments: