/*
  // FileIO.java
  Basic File Input/Output classes
    by milan ramaiya
    
  last changed: 2001.11.20

  API:
  ----  
  class FileIn:                     allows input from file
    CONSTRUCTORS
    ------------
    FileIn(String filename)         opens <filename> for input
      
    METHODS
    -------
    int      read()                 reads next byte as int
    char     readChar()             reads next byte as char
    byte     readByte()             reads next byte as byte
    String   readLine()             reads until eol or eof encountered
    String   readString()           reads remainder of file as string
    String   readString(int maxlen) reads up to <maxlen> characters into string
    String   readString(char stop)  reads until <stop> encountered
    String[] readLines()            reads remaining lines as string array

    STATIC METHODS
    --------------
    String   readStrin(String filename) reads contents as string
    String[] readLines(String filename) reads contents as string array

  class FileOut:                     allows output to file
    CONSTRUCTORS
    ------------
    FileOut(String filename)         opens <filename> for output

    METHODS
    -------
    close()                           close output stream
    flush()                           flush output stream
    print(ANY)                        print
    println(ANY)                      print

    STATIC METHODS
    --------------
    print(String filename, String s)  write <s> into <filename>
*/

import java.io.*;
import java.util.*;

class FileIn
{
    BufferedReader fin;

    // Constructor
    FileIn(String filename) throws FileNotFoundException
    { fin = new BufferedReader(new FileReader(filename)); }

    // General read() functions
    public int    read()     throws IOException { return fin.read();       }
    public char   readChar() throws IOException { return (char)fin.read(); }
    public byte   readByte() throws IOException { return (byte)fin.read(); }
    public String readLine() throws IOException { return fin.readLine();   }
    
    // Reads the remainder of the file as a string
    public String readString() throws IOException
    {
        String s = new String();
        int i;
        while( (i=fin.read())!=-1 )
            s+=(char)i;
        return s;
    }

    // Reads a specified number of characters as a string
    public String readString(int maxlen) throws IOException
    {
        char c[] = new char[maxlen];
        fin.read(c, 0, maxlen);
        return new String(c);
    }

    // Reads a string until a specific character is found
    public String readString(char stop) throws IOException
    {
        String s = new String();
        char c;
        while( (c=(char)fin.read())!=stop )
            s+=c;
        return s;
    }

    // Reads all remaining lines into a string array
    public String[] readLines() throws IOException
    {
        Vector vdata = new Vector();
        String line;
        while( (line=fin.readLine())!=null )
            vdata.add(line);

        String data[] = new String[vdata.size()];
        for(int i=0; i<vdata.size(); i++)
            data[i]=(String)vdata.get(i);
        return data;
    }

    /*
      STATIC METHODS
    */    
    public static String readString(String filename)
        throws FileNotFoundException, IOException
    { return (new FileIn(filename)).readString(); }
    
    public static String[] readLines(String filename)
        throws FileNotFoundException, IOException
    { return (new FileIn(filename)).readLines();  }
}

class FileOut
{
    PrintWriter fout;
    
    FileOut(String filename) throws IOException
    { fout = new PrintWriter(
                 new BufferedWriter(new FileWriter(filename)), true); }

    public void close()           throws IOException { fout.close();    }
    public void flush()           throws IOException { fout.flush();    }
    public void print(char c)     throws IOException { fout.print(c);   }
    public void print(String s)   throws IOException { fout.print(s);   }
    public void print(int i)      throws IOException { fout.print(i);   }
    public void print(long l)     throws IOException { fout.print(l);   }
    public void print(float f)    throws IOException { fout.print(f);   }
    public void print(double d)   throws IOException { fout.print(d);   }
    public void print(Object o)   throws IOException { fout.print(o);   }
    public void println()         throws IOException { fout.println();  }
    public void println(char c)   throws IOException { fout.println(c); }
    public void println(String s) throws IOException { fout.println(s); }
    public void println(int i)    throws IOException { fout.println(i); }
    public void println(long l)   throws IOException { fout.println(l); }
    public void println(float f)  throws IOException { fout.println(f); }
    public void println(double d) throws IOException { fout.println(d); }
    public void println(Object o) throws IOException { fout.println(o); }
    
    /*
      STATIC METHODS
    */
    public static void print(String filename, String s) throws IOException
    {
        FileOut fo = new FileOut(filename);
        fo.print(s);
        fo.flush();
    }

    public static void write(String filename, String s) throws IOException
    { print(filename, s); }
}
