Escribir al final de un fichero

1987 vistas

Para escribir al final de un fichero tendremos que usar la clase java.io.FileOutputStream. Esta clase tiene constructores que tienen como argumento un booleano (append) que si vale true los datos escritos se añadirán al final.

Veamos un ejemplo:



java
  1.     FileOutputStream fos = null;
  2.     try
  3.     {
  4.       fos = new FileOutputStream("filename.txt", true);
  5.       // ...
  6.     }
  7.     catch (FileNotFoundException e)
  8.     {
  9.       // ...
  10.     }
  11.     finally
  12.     {
  13.       try
  14.       {
  15.         fos.close();
  16.       }
  17.       catch (IOException e)
  18.       {
  19.         // ...
  20.       }
  21.     }