Reading a Text File & Handling Text Data
The below command line program demonstrates how to use a Scanner to read a text file into memory and create a simple Regular Expression (RegEx) to break a text file into words.
Main.java
package ceccs;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws Exception{
//read file from resources dir
//use FileChooser in JFX for browse btn
String filePath = Main.class.getResource("/test-gibberish.txt").getFile();
File testTextFile = new File(filePath);
//create a scanner to read the file
Scanner scnr = new Scanner(testTextFile);
//Example 1
String fileContents = new String();
while(scnr.hasNextLine()) {
fileContents += scnr.nextLine() + "\n";
}
// fileContents is recreated but has one extra line return at end
// trim last line return.
fileContents = fileContents.substring(0,fileContents.length()-2);
// test print file contents - it should match the original file exactly
System.out.println("-----FILE CONTENTS STRING-----------");
System.out.println(fileContents);
// Example 2 break the file contents into words - an array list of strings
// Note: scanner must be 'reset' to beginning of file by remaking it since it
// reached end of file.
Scanner stringScnr = new Scanner(fileContents);
scnr.useDelimiter(Pattern.compile("\\W"));
ArrayList<String> words = new ArrayList();
while(stringScnr.hasNext()){
words.add(stringScnr.next());
}
System.out.println("\n\n-----ARRAYLIST OF WORD STRINGS--------");
// testing print the list of words
for(int i = 0; i < words.size(); i++){
System.out.println(words.get(i));
}
}
}