FileProcessing.java

  1. package org.drip.zen.juice;

  2. import java.io.BufferedReader;
  3. import java.io.FileReader;

  4. /*
  5.  * 1) Number of lines in the file
  6.  * 2) Read the File into a String Array
  7.  * 3) Count the Number of Words in a Single Line
  8.  * 4) Count the Number Words across the full File
  9.  * 5) Display the File Contents
  10.  * 6) Some Notes on Error Handling
  11.  * 7) Exercise - Count the Number of Instances of Occurrence of a Word (example, Romeo)
  12.  * 8) Exercise - Find out the Line that has the most Occurrence of the given Word
  13.  */

  14. public class FileProcessing {

  15.     static int NumberOfLinesInFile (String fullFileName)
  16.         throws Exception
  17.     {
  18.         String line = "";
  19.         int numberOfLines = 1;
  20.         boolean stopReading = false;

  21.         BufferedReader reader = new BufferedReader (new FileReader (fullFileName));

  22.         while (stopReading == false)
  23.         {
  24.             line = reader.readLine();

  25.             if (line == null)
  26.             {
  27.                 stopReading = true;
  28.             }
  29.             else
  30.             {
  31.                 numberOfLines = numberOfLines + 1;
  32.             }
  33.         }

  34.         reader.close();

  35.         return numberOfLines;
  36.     }

  37.     static String[] ReadFile (String fullFileName)
  38.         throws Exception
  39.     {
  40.         int numberOfLinesToRead = NumberOfLinesInFile (fullFileName);

  41.         String[] fileLines = new String[numberOfLinesToRead];

  42.         String line = "";
  43.         int lineNumber = 0;
  44.         boolean stopReading = false;

  45.         BufferedReader reader = new BufferedReader (new FileReader (fullFileName));

  46.         while (stopReading == false)
  47.         {
  48.             line = reader.readLine();

  49.             if (line == null)
  50.                 stopReading = true;
  51.             else
  52.             {
  53.                 fileLines[lineNumber] = line;
  54.                 lineNumber = lineNumber + 1;
  55.             }
  56.         }

  57.         reader.close();

  58.         return fileLines;
  59.     }

  60.     static int WordCount (String singleLine)
  61.     {
  62.         if (singleLine == null)
  63.         {
  64.             return 0;
  65.         }

  66.         int count = 1;

  67.         for (int letterIndex = 0; letterIndex < singleLine.length(); letterIndex = letterIndex + 1)
  68.         {
  69.             char letter = singleLine.charAt (letterIndex);

  70.             if (letter == ' ')
  71.             {
  72.                 count = count + 1;
  73.             }
  74.         }

  75.         return count;
  76.     }

  77.     static String[] Words (String singleLine)
  78.     {
  79.         if (singleLine == null)
  80.         {
  81.             return null;
  82.         }

  83.         String[] wordsInLine = singleLine.split (" ");

  84.         return wordsInLine;
  85.     }

  86.     public static final void main (String[] input)
  87.         throws Exception
  88.     {
  89.         String fileLocation = "C:\\DRIP\\CreditAnalytics\\Daemons\\Feeds\\TextMiner\\RomeoAndJuliet.txt";

  90.         int fileLineCount = NumberOfLinesInFile (fileLocation);

  91.         String[] fileContents = ReadFile (fileLocation);

  92.         int totalWords = 0;

  93.         for (int i = 0; i < fileLineCount; i = i + 1)
  94.         {
  95.             String currentLine = fileContents[i];

  96.             int numberOfWordsInCurrentLine = WordCount (currentLine);

  97.             totalWords = totalWords + numberOfWordsInCurrentLine;

  98.             String[] wordsInCurrentLine = Words (currentLine);

  99.             String wordDump = "";

  100.             for (int j = 0; j < numberOfWordsInCurrentLine; j = j + 1)
  101.             {
  102.                 wordDump = wordDump + wordsInCurrentLine[j] + ",";
  103.             }

  104.             System.out.println (wordDump);
  105.         }

  106.         System.out.println ("\tNumber of Lines in File: " + fileLineCount);

  107.         System.out.println ("\tNumber of Words in File: " + totalWords);
  108.     }
  109. }