/*
 * TextFileWriter.java  1.0   Michael Zanussi
 *
 * Copyright (C) 2004 Michael Zanussi
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 */

package com.michaelzanussi.bayesian;

import java.io.*;

/**
 * Wrapper class around Java's BufferedWriter class.
 
 @author <a href="mailto:admin@michaelzanussi.com">Michael Zanussi</a>
 @version 1.0 (20 Feb 2004) 
 */
public class TextFileWriter implements BasicIO {

  // The output buffer.
  private BufferedWriter buffer;
     
  /**
   * No-arg constructor.
   */
  public TextFileWriter() { 
    
    buffer = null;
    
  }
  
  /**
   * Opens the specified file for writing.
   
   @param file the file to open.
   @return <code>true</code> if successful.
   */
  public boolean openFile file ) {

    try {
      buffer = new BufferedWriternew FileWriterfile ) );
      return true;
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
      System.exit);
    }
    
    return false;
    
  }

  /**
   * Closes the file writer.
   
   @return <code>true</code> if successful.
   */
  public boolean close() {
    
    try {
      ifbuffer != null ) {
        buffer.close();
        return true;
      }
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
    return false;
    
  }
  
  /**
   * Writes a string to the buffer, without a newline.
   
   @param string the string to write to the buffer.
   */
  public void writeString string ) {
    
    try {
      buffer.writestring );
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
  }

  /**
   * Writes a string to the buffer, followed by a newline.
   
   @param string the string to write to the buffer.
   */
  public void writelnString string ) {
    
    try {
      buffer.writestring );
      buffer.newLine();
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
  }

  /**
   * Writes a newline to the buffer.
   */
  public void writeln() {
    
    try {
      buffer.newLine();
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
  }

}