|
/* import all packages within the io namespace. (Mostly handles input/output operations) |
|
* import all packages within the util namespace. (Include all Utilities) |
|
*/ |
|
import java.io.*; |
|
import java.util.*; |
|
|
|
|
|
/* |
|
* The general format of a class is the [accessModifier] class [className] {} |
|
* |
|
* A class is the basic building block of all object oriented programming architecture. |
|
* It's simply a blueprint, from which new objects can be instantiated (created) from. |
|
* |
|
* A class contains variables, properties and methods |
|
*/ |
|
public class TextFileReader |
|
{ |
|
/* |
|
* This is the main method. It serves as the entry point into your |
|
* application. It is required for your application to run |
|
* |
|
* During Runtime, it is possible for our application to throw an exception, |
|
* So we have to let our system know, so as to avoid compilation errors. |
|
*/ |
|
public static void main (String[] args) |
|
{ |
|
/* |
|
* First, we create a string variable called filename and assign it a value of null. |
|
* |
|
* We know an Exception (Error at Runtime) may occur, so we wrap our executing code within |
|
* a try catch block so as to decide how we'll handle it if and when it occurs. |
|
* |
|
* We grab the filename from the arguments passed or the keyboard input and store it in filename variable |
|
* |
|
* Next We call a function that is dedicated towards getting information about the file, while |
|
* passing it the filename to read from. |
|
* |
|
*/ |
|
|
|
String filename = null; |
|
|
|
try { |
|
|
|
filename = (args.length != 0) ? args[0] : getFileToReadFromKeyboardInput(); |
|
|
|
getInformationAboutFile(filename); |
|
|
|
} catch (java.io.IOException e) { |
|
|
|
System.out.println ("Sorry, \"" + filename + "\" does not exist in the file location specified."); |
|
System.out.println ("Note: \"" + filename + "\" should exist relative to the java file location."); |
|
} |
|
} |
|
|
|
/* |
|
* This method is dedicated to grabbing the filename and location of the file to read from. |
|
* and then returning the filename so we can pass it into the next function |
|
*/ |
|
private static String getFileToReadFromKeyboardInput() throws java.io.IOException |
|
{ |
|
// set up the buffered reader to read from the keyboard |
|
BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); |
|
|
|
System.out.println ("Enter file name (file path should be relative to java file location):"); |
|
|
|
String filename = br.readLine(); // Read line when Input from keyboard has been entered |
|
|
|
return filename; |
|
} |
|
|
|
/* |
|
* This method grabs information about the supplied filename. |
|
* and may throw an IO Exception if the operation is unsuccessful. |
|
* Information such as Character Length, and how many words etc. |
|
*/ |
|
private static void getInformationAboutFile(String filename) throws java.io.IOException |
|
{ |
|
String firstLine; |
|
String word; |
|
|
|
// set up the buffered reader to read from a file |
|
BufferedReader br = new BufferedReader (new FileReader (filename)); |
|
|
|
firstLine = br.readLine(); // Read the entire line |
|
|
|
System.out.println ("\n\nThe first line of \"" + filename + "\" contains \"" + firstLine + "\""); |
|
System.out.println ("The first line has " + firstLine.length() + " characters.\n"); |
|
|
|
System.out.println ("Breaking the lines into words we get:"); |
|
|
|
// We use a StringTokenizer Class to break the sentence into individual words |
|
int numTokens = 0; |
|
StringTokenizer st = new StringTokenizer (firstLine); |
|
|
|
while (st.hasMoreTokens()) |
|
{ |
|
word = st.nextToken(); |
|
numTokens++; |
|
System.out.println (" Word " + numTokens + " is: " + word); |
|
} |
|
} |
|
} |