Oi Carlos,
 
Utilize o RandomAcessFile ao inves de FileWriter, segue codigo-fonte:
 
import java.lang.*;
import java.io.*;
 
import java.util.Calendar;
 
public class Log {
 
 private static File outputFile;
 private static RandomAccessFile out;
 
 public static void create(){
  Calendar date = Calendar.getInstance();
 
  String diapend  = "";
 
  Integer dia = new Integer(date.get(date.DAY_OF_MONTH));
  Integer mes = new Integer(date.get(date.MONTH) + 1);
  Integer ano = new Integer(date.get(date.YEAR));
 
  if (ano.toString().length() < 4)
   diapend += "2001";
  else
   diapend += ano.toString();
  if (mes.toString().length() < 2)
   diapend += "0" + mes.toString();
  else
   diapend += mes.toString();
  if (dia.toString().length() < 2)
   diapend += "0" + dia.toString();
  else
   diapend += dia.toString();
 
  outputFile = new File("rmiserver" + diapend + ".log");
 }
 
 public static void open() throws IOException{
  if (!outputFile.exists())
    outputFile.createNewFile();
  out = new RandomAccessFile(outputFile, "rw");
  out.seek(outputFile.length());
 }
 
 public static void write(String str) throws IOException{
  str += "\n";
  out.write(str.getBytes());
 }
 
 public static void close()throws IOException{
  out.write("\n".getBytes());
  out.close();
 }
}
 
John Tau,
Software Express Informática.
 
Rua Cerro Corá, 1208 sala 28
+ 55-11-3024-5333 ramal 5257
 
----- Original Message -----
Sent: Tuesday, August 28, 2001 10:48 AM
Subject: [java-list] - Problemas ao gravar em TXT

Ola a todos,
 
Estou desenvolvendo uma classe que utilizarei para salvar mensagens específicas em um arquivo de Log (.txt) em uma intranet local que temos aqui na empresa.
É a primeira vez que trabalho com o package java.io, e quando o coloco para escrever no arquivo TXT, ele sempre sobrescreve o que já está escrito no mesmo arquivo, ao invés de adicionar uma nova linha.
Meu código é o seguinte:
 
     try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(nomearquivo.txt)));
            out.println("Mensagem específica");
            out.close();
         } catch (IOException evt) {
            //Codigo para lidar com a Exceção
         }
 
Existe algum erro ou incoerência no código acima?
Pensei em criar um BufferedReader, ou outra classe semelhante para varrer o arquivo até o final e só depois então começar a escrever, isso é possivel? Como poderia ser feito? Não teria uma queda de performance ou coisa parecida?
 
Se alguém puder me ajudar...
Grato desde já.
 
          
Atenciosamente
 
[Carlos H.]
[EMAIL PROTECTED]

Responder a