Advertisement
calcpage

APCS_CH8_EasyWriter.java

Mar 13th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.91 KB | None | 0 0
  1. import java.io.PrintWriter;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Formatter;
  5.  
  6. /**
  7.  * @author Gary Litvin
  8.  * @version 1.6, 01/01/11
  9.  *
  10.  * Appendix to:
  11.  *
  12.  * <i>Java Methods: Object-Oriented Programming and Data Structures</i>
  13.  * (Skylight Publishing 2011, ISBN 978-0-9824775-7-1)
  14.  *
  15.  * EasyWriter provides simple methods for opening and
  16.  * writing to text files.  All exceptions are handled
  17.  * inside the class and are hidden from the user.
  18.  *
  19.    <xmp>
  20.    Example:
  21.    =======
  22.  
  23.        EasyWriter outputFile = new EasyWriter("anyname.txt");
  24.        if (outputFile.bad())
  25.        {
  26.          System.err.println("*** Cannot create anyname.txt ***");
  27.          System.exit(1);
  28.        }
  29.        outputFile.print("2 + 2 = ");
  30.        outputFile.println(4);
  31.        outputFile.printf("2 + 2 = %d\n", 4);
  32.        outputFile.println();  // an extra blank line
  33.        outputFile.close();
  34.  
  35.        outputFile = new EasyWriter("anyname.txt", "app");
  36.        if (outputFile.bad())
  37.        {
  38.          System.err.println("*** Cannot create anyname.txt ***");
  39.          System.exit(1);
  40.        }
  41.        outputFile.printf("pi = %5.2f\n", Math.pi);
  42.        outputFile.close();
  43.  
  44.    </xmp>
  45.  */
  46. public class EasyWriter
  47. {
  48.   private String myFileName;
  49.   private PrintWriter myOutFile;
  50.   private int myErrorFlags = 0;
  51.   private static final int OPENERROR = 0x0001;
  52.   private static final int CLOSEERROR = 0x0002;
  53.   private static final int WRITEERROR = 0x0004;
  54.   private Formatter formatter;
  55.  
  56.   /**
  57.    * Constructs an <code>EasyWriter</code> associated with a new file
  58.    * (or truncates an existing file).
  59.    * @param fileName the name of the file to be created.
  60.    */
  61.   public EasyWriter(String fileName)
  62.   {
  63.     this(fileName, null);
  64.   }
  65.  
  66.   /**
  67.    * Constructs an <code>EasyWriter</code> that can append data to an
  68.    * existing file.
  69.    * @param fileName the name of the file to be created.
  70.    * @param mode if <code>mode</code> is "app" and the file exists,
  71.    * then opens the file in append mode.
  72.    */
  73.   public EasyWriter(String fileName, String mode)
  74.   {
  75.     myFileName = fileName;
  76.     myErrorFlags = 0;
  77.  
  78.     try
  79.     {
  80.       myOutFile = new PrintWriter(
  81.               new FileWriter(fileName, "app".equals(mode)));
  82.     }
  83.     catch (IOException e)
  84.     {
  85.       myErrorFlags |= OPENERROR;
  86.       myFileName = null;
  87.     }
  88.   }
  89.  
  90.   /**
  91.    * Closes the file.  If the file is not closed, some data may remain
  92.    * in the write buffer but not written to the file.
  93.    */
  94.   public void close()
  95.   {
  96.     if (myFileName != null)
  97.     {
  98.       myOutFile.close();
  99.     }
  100.     formatter = null;
  101.     myFileName = null;
  102.   }
  103.  
  104.   /**
  105.    * Checks the status of the file.
  106.    * @return true if an error occurred when opening or writing to the file;
  107.    * false otherwise.
  108.    */
  109.   public boolean bad()
  110.   {
  111.     return myErrorFlags != 0;
  112.   }
  113.  
  114.   /**
  115.    * Writes one character to the file.
  116.    * @param ch character to be written.
  117.    */
  118.   public void print(char ch)
  119.   {
  120.     myOutFile.print(ch);
  121.   }
  122.  
  123.   /**
  124.    * Writes an integer to the file.
  125.    * @param k number to be written.
  126.    */
  127.   public void print(int k)
  128.   {
  129.     myOutFile.print(k);
  130.   }
  131.  
  132.   /**
  133.    * Writes a double to the file.
  134.    * @param x number to be written.
  135.    */
  136.   public void print(double x)
  137.   {
  138.     myOutFile.print(x);
  139.   }
  140.  
  141.   /**
  142.    * Writes a string to the file.
  143.    * @param s string to be written.
  144.    */
  145.   public void print(String s)
  146.   {
  147.     myOutFile.print(s);
  148.   }
  149.  
  150.   /**
  151.    * Writes an object to the file.
  152.    * @param obj object to be written.
  153.    */
  154.   public void print(Object obj)
  155.   {
  156.     myOutFile.print(obj);
  157.   }
  158.  
  159.   /**
  160.    * Writes a newline character to the file.
  161.    */
  162.   public void println()
  163.   {
  164.     myOutFile.println();
  165.   }
  166.  
  167.   /**
  168.    * Writes one character and newline to the file.
  169.    * @param ch character to be written.
  170.    */
  171.   public void println(char ch)
  172.   {
  173.     myOutFile.println(ch);
  174.   }
  175.  
  176.   /**
  177.    * Writes an integer and newline to the file.
  178.    * @param k number to be written.
  179.    */
  180.   public void println(int k)
  181.   {
  182.     myOutFile.println(k);
  183.   }
  184.  
  185.   /**
  186.    * Writes a double and newline to the file.
  187.    * @param x number to be written.
  188.    */
  189.   public void println(double x)
  190.   {
  191.     myOutFile.println(x);
  192.   }
  193.  
  194.   /**
  195.    * Writes a string and newline to the file.
  196.    * @param s string to be written.
  197.    */
  198.   public void println(String s)
  199.   {
  200.     myOutFile.println(s);
  201.   }
  202.  
  203.   /**
  204.    * Writes an object and newline to the file.
  205.    * @param obj object to be written.
  206.    */
  207.   public void println(Object obj)
  208.   {
  209.     myOutFile.println(obj);
  210.   }
  211.  
  212.   /**
  213.    * A convenience method to write a formatted string to this
  214.    * <code>EasyWriter</code> using the specified format string and parameters.
  215.    */
  216.   public void printf(String format, Object ... args)
  217.   {
  218.     if (formatter == null)
  219.       formatter = new Formatter(myOutFile);
  220.     formatter.format(format, args);
  221.   }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement