Skip to content

Instantly share code, notes, and snippets.

@CSEmbree
Created May 7, 2014 01:12
Show Gist options
  • Save CSEmbree/661033e5b4287f73c9fc to your computer and use it in GitHub Desktop.
Save CSEmbree/661033e5b4287f73c9fc to your computer and use it in GitHub Desktop.
hello;there;senor
how
are
you;today
/*
* Example opening and reading a text file in Java using:
* File, Scanner, StringTokenizer
*
*/
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
class test {
public static void main(String[] args) {
String fName = "data.txt";
try{
File f = new File(fName);
Scanner s = new Scanner( f );
String delim = ";"; // space delimiterized
String sentence = ""; //each sentence
String word = ""; //each word of a sentence
while( s.hasNext() == true) {
sentence = s.next();
StringTokenizer tok = new StringTokenizer(sentence, delim);
while (tok.hasMoreTokens() == true) {
word = tok.nextToken();
System.out.println("word: " + word);
}
}
} catch (FileNotFoundException fnfe) {
System.out.println("ERROR: File '" + fName + "'!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment