Advertisement
Guest User

textfileinput.java

a guest
Mar 6th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.65 KB | None | 0 0
  1. package com.company;
  2.  
  3. /**
  4.  * Created by michaelkantor on 2/24/15.
  5.  */
  6. // TextFileInput.java
  7. // Copyright (c) 2000, 2005 Dorothy L. Nixon.  All rights reserved.
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.FileInputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.IOException;
  13.  
  14. /**
  15.  * Simplified buffered character input
  16.  * stream from an input text file.
  17.  * Manages an input text file,
  18.  * handling all IOExceptions by generating
  19.  * RuntimeExcpetions (run-time error
  20.  * messages).
  21.  *
  22.  * If the text file cannot be created,
  23.  * a RuntimeException is thrown,
  24.  * which by default results an an
  25.  * error message being printed to
  26.  * the standard error stream.
  27.  *
  28.  * @author D. Nixon
  29.  */
  30. public class TextFileInput  {
  31.  
  32.     /**  Name of text file  */
  33.     private String filename;
  34.  
  35.     /**  Buffered character stream from file  */
  36.     private BufferedReader br;
  37.  
  38.     /**  Count of lines read so far.  */
  39.     private int lineCount = 0;
  40.  
  41.     /**
  42.      * Creates a buffered character input
  43.      * strea, for the specified text file.
  44.      *
  45.      * @param filename the input text file.
  46.      * @exception RuntimeException if an
  47.      *          IOException is thrown when
  48.      *          attempting to open the file.
  49.      */
  50.     public TextFileInput(String filename)
  51.     {
  52.         this.filename = filename;
  53.         try  {
  54.             br = new BufferedReader(
  55.                     new InputStreamReader(
  56.                             new FileInputStream(filename)));
  57.         } catch ( IOException ioe )  {
  58.             throw new RuntimeException(ioe);
  59.         }  // catch
  60.     }  // constructor
  61.  
  62.     /**
  63.      * Closes this character input stream.
  64.      * No more characters can be read from
  65.      * this TextFileInput once it is closed.
  66.      * @exception NullPointerException if
  67.      *        the file is already closed.
  68.      * @exception RuntimeException if an
  69.      *       IOException is thrown when
  70.      *       closing the file.
  71.      */
  72.     public void close()
  73.     {
  74.         try  {
  75.             br.close();
  76.             br = null;
  77.         } catch ( NullPointerException npe )  {
  78.             throw new NullPointerException(
  79.                     filename + "already closed.");
  80.         } catch ( IOException ioe )  {
  81.             throw new RuntimeException(ioe);
  82.         }  // catch
  83.     }  // method close
  84.  
  85.     /**
  86.      * Reads a line of text from the file and
  87.      * positions cursor at 0 for the next line.
  88.      * Reads from the current cursor position
  89.      * to end of line.
  90.      * Implementation does not invoke read.
  91.      *
  92.      * @return the line of text, with
  93.      *         end-of-line marker deleted.
  94.      * @exception RuntimeException if an
  95.      *          IOException is thrown when
  96.      *          attempting to read from the file.
  97.      */
  98.     public String readLine()
  99.     {
  100.  
  101.         return readLineOriginal();
  102.     }  // method readLine()
  103.  
  104.     /**
  105.      * Returns a count of lines
  106.      * read from the file so far.
  107.      */
  108.     public int getLineCount()  {
  109.         return lineCount;
  110.     }
  111.  
  112.     /**
  113.      * Tests whether the specified character is equal,
  114.      * ignoring case, to one of the specified options.
  115.      *
  116.      * @param toBeChecked the character to be tested.
  117.      * @param options a set of characters
  118.      * @return true if <code>toBeChecked</code> is
  119.      *         equal, ignoring case, to one of the
  120.      *         <code>options</code>, false otherwise.
  121.      */
  122.     public static boolean isOneOf(char toBeChecked,
  123.                                   char[] options)
  124.     {
  125.         boolean oneOf = false;
  126.         for ( int i = 0; i < options.length && !oneOf; i++ )
  127.             if ( Character.toUpperCase(toBeChecked)
  128.                     == Character.toUpperCase(options[i]) )
  129.                 oneOf = true;
  130.         return oneOf;
  131.     }  // method isOneOf(char, char[])
  132.  
  133.     /**
  134.      * Tests whether the specified string is one of the
  135.      * specified options.  Checks whether the string
  136.      * contains the same sequence of characters (ignoring
  137.      * case) as one of the specified options.
  138.      *
  139.      * @param toBeChecked the String to be tested
  140.      * @param options a set of Strings
  141.      * @return true if <code>toBeChecked</code>
  142.      *         contains the same sequence of
  143.      *         characters, ignoring case, as one of the
  144.      *         <code>options</code>, false otherwise.
  145.      */
  146.     public static boolean isOneOf(String toBeChecked,
  147.                                   String[] options)
  148.     {
  149.         boolean oneOf = false;
  150.         for ( int i = 0; i < options.length && !oneOf; i++ )
  151.             if ( toBeChecked.equalsIgnoreCase(options[i]) )
  152.                 oneOf = true;
  153.         return oneOf;
  154.     }  // method isOneOf(String, String[])
  155.  
  156.     /**
  157.      * Reads a line from the text file and ensures that
  158.      * it matches one of a specified set of options.
  159.      *
  160.      * @param options array of permitted replies
  161.      *
  162.      * @return the line of text, if it contains the same
  163.      *         sequence of characters (ignoring case for
  164.      *         letters) as one of the specified options,
  165.      *         null otherwise.
  166.      * @exception RuntimeException if the line of text
  167.      *         does not match any of the specified options,
  168.      *         or if an IOException is thrown when reading
  169.      *         from the file.
  170.      * @exception NullPointerException if no options are
  171.      *         provided, or if the end of the file has been
  172.      *         reached.
  173.      */
  174.     public String readSelection(String[] options)
  175.     {
  176.         if ( options == null || options.length == 0 )
  177.             throw new NullPointerException(
  178.                     "No options provided for "
  179.                             + " selection to be read in file "
  180.                             + filename + ", line "
  181.                             + (lineCount + 1) + ".");
  182.  
  183.         String answer = readLine();
  184.  
  185.         if ( answer == null )
  186.             throw new NullPointerException(
  187.                     "End of file "
  188.                             + filename + "has been reached.");
  189.  
  190.         if ( !TextFileInput.isOneOf(answer, options) )  {
  191.             String optionString = options[0];
  192.             for ( int i = 1; i < options.length; i++ )
  193.                 optionString += ", " + options[i];
  194.             throw new RuntimeException("File " + filename
  195.                     + ", line " + lineCount
  196.                     + ": \"" + answer
  197.                     + "\" not one of "
  198.                     + optionString + ".");
  199.         }  // if
  200.         return answer;
  201.     }  // method readSelection
  202.  
  203.     /**
  204.      * Reads a line from the text file and ensures that
  205.      * it matches, ignoring case, one of "Y", "N", "yes",
  206.      * "no", "1", "0", "T", "F", "true", or "false".
  207.      * There must be no additional characters on the line.
  208.      *
  209.      * @return <code>true</code> if the line matches
  210.      *        "Y", "yes", "1" "T", or "true".
  211.      *        <code>false</code> if the line matches
  212.      *        "N", "no", "0", "F", or "false".
  213.      * @exception RuntimeException if the line of text
  214.      *        does not match one of "Y", "N", "yes",
  215.      *        "no", "1", "0", "T", "F", "true", or "false",
  216.      *        or if an IOException is thrown when reading
  217.      *        from the file.
  218.      * @exception NullPointerException if the end of the
  219.      *        file has been reached.
  220.      */
  221.     public boolean readBooleanSelection()
  222.     {
  223.         String[] options = {"Y", "N", "yes", "no", "1", "0",
  224.                 "T", "F", "true", "false"};
  225.         String answer = readSelection(options);
  226.         return isOneOf(answer,
  227.                 new String[] {"Y", "yes", "1", "T", "true"} );
  228.     }  // method askUserYesNo
  229.  
  230.     /**
  231.      * Reads a line of text from the file and
  232.      * increments line count.  (This method
  233.      * is called by public readLine and is
  234.      * final to facilitate avoidance of side
  235.      * effects when public readLine is overridden.)
  236.      *
  237.      * @return the line of text, with
  238.      *         end-of-line marker deleted.
  239.      * @exception RuntimeException if an
  240.      *          IOException is thrown when
  241.      *          attempting to read from the file.
  242.      */
  243.     protected final String readLineOriginal()
  244.     {
  245.         try  {
  246.             if ( br == null )
  247.                 throw new RuntimeException(
  248.                         "Cannot read from closed file "
  249.                                 + filename + ".");
  250.             String line = br.readLine();
  251.             if ( line != null )
  252.                 lineCount++;
  253.             return line;
  254.         } catch (IOException ioe)  {
  255.             throw new RuntimeException(ioe);
  256.         }  // catch
  257.     }  // method readLineOriginal
  258. }  // class TextFileInput
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement