Skip to content

Instantly share code, notes, and snippets.

@dammyammy
Last active August 21, 2016 14:23
Show Gist options
  • Save dammyammy/b023bbe560426ae87bf09e663831735b to your computer and use it in GitHub Desktop.
Save dammyammy/b023bbe560426ae87bf09e663831735b to your computer and use it in GitHub Desktop.
An Extremely Simple Text File Reader Written in Java. (As a beginner's Tutorial)
I just read from this file. Isn't java just Awesome? Thanks Dammy.
/* 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);
}
}
}

Compile Java Class

 javac TextFileReader.java

Usage

Option 1: Arguement Based Usage

 java TextFileReader test.txt

Option 2: Command line Input Based Usage

 java TextFileReader

Then follow the prompt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment