Write to the end of a fileTag(s): IO


[JDK1.0.2]
import java.io.*;
public class appendtext {
  public static void main(String args[]){
  try { 
    PrintStream out = 
        new PrintStream(new AppendFileStream("myfile"));
    out.print("A new line of text");
    out.close();
    }
   catch(Exception e) {
    System.out.println(e.toString());
    }
   }
}

class AppendFileStream extends OutputStream {
   RandomAccessFile fd;
   public AppendFileStream(String file) throws IOException {
     fd = new RandomAccessFile(file,"rw");
     fd.seek(fd.length());
     }
   public void close() throws IOException {
     fd.close();
     }
   public void write(byte[] b) throws IOException {
     fd.write(b);
     }
   public void write(byte[] b,int off,int len) throws IOException {
     fd.write(b,off,len);
     }
   public void write(int b) throws IOException {
     fd.write(b);
     }
}
[JDK1.1]
FileOutputStream fos = new FileOutputStream("myfile", true);
to open in Append mode
blog comments powered by Disqus