import java.io.*;
import javax.swing.*;

public class LeituraTxt

{
  public static final int READ  = 0;
  public static final int WRITE = 1;
  public static final int NONE  = 2;
  private String fname;
  private int modo = NONE;
  private FileReader arqreader = null;
  private FileWriter arqwriter = null;
  
public void open(String fn,int m){
     modo = m;
     fname = fn;
     try{
       if (modo == READ)
         arqreader = new FileReader(fname);
       else
         arqwriter = new FileWriter(fname,true);
     }catch(IOException e){
         System.out.println(e.getMessage());
         modo = NONE;
     }
  }            

public void create(String fn){
    modo = WRITE;
    fname = fn;
    try{
    	arqwriter = new FileWriter(fname);
    }
    catch(IOException e){
      System.out.println(e.getMessage());
      modo = NONE;
    }
}	

public void write(String s){
  try{
     arqwriter.write(s+'\n');
  }
   catch(IOException e){     
        System.out.println(e.getMessage( )); }
}

public String read(){
  try{
       String buf = "";   int c; 
       do{
           c = arqreader.read();
           if (c == -1) return(null);
           if (c != '\n') buf = buf + (char)c;
     //      System.out.println ("Caracter lido "+ (char)c);
     //      System.out.println("Conteudo atual do buffer: "+buf);
         }
         while(arqreader.ready() && (c != '\n'));
        // System.out.println(buf.length());
         buf = buf.substring(0,buf.length()-1);
        // System.out.println(buf.length());
       return(buf);
     }catch(IOException e){
           System.out.println(e.getMessage());
           return("eof");
     }
}

public void close(){
  try{
    if (modo == READ) arqreader.close();
    if (modo == WRITE) arqwriter.close();
  }catch(IOException e){
      System.out.println(e.getMessage()); 
   }
  arqreader = null; arqwriter = null; modo = NONE;
}

}			