  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  public class FileWriter implements OutChannel {
  
    /**
     * The file where data will be written.
     */
    private FileOutputStream dstFile;
  
    /**
     * Constructor.
     * @param fileName the name of the file where data will be written.
     * @throws FileNotFoundException In case we fail to open the file.
     */
    public FileWriter(final String fileName) throws FileNotFoundException {
      dstFile = new FileOutputStream(new File(fileName));
    }
  
    /**
     * Write the data into the file.
     * @param data the data that will be written into the file.
     * @throws IOException In case we fail to write the data.
     */
    public final void write(final int data) throws IOException {
      dstFile.write(data);
    }
  
    /**
     * Call it once we are done sending the data.
     * @throws IOException in case we fail to close the channel.
     */
    public final void done() throws IOException {
      dstFile.close();
    }
  
    @Override
    public final void finalize() {
      try {
        done();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
