Olá Ary,
 
Veja o texto retirado da Documentacao do Java.

writeUTF

public final void writeUTF(String str)
                    throws IOException
Writes a string to the file using UTF-8 encoding in a machine-independent manner.

First, two bytes are written to the file, starting at the current file pointer, as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for each character.

Specified by:
writeUTF in interface DataOutput
Parameters:
str - a string to be written.
Throws:
IOException - if an I/O error occurs.
Ou seja, primeiro dois bytes sao escritos para a posicao corrente do arquivo, logo após a sua string será, gravada no arquivo. note que o tamanho do arquivo será 7 ou seja os seus 5 caracteres mais os dois bytes que o metodo grava
 
Um Abraco
 
Isaías Cristiano Barroso
 
 
----- Original Message -----
From: Ary Junior
Sent: Thursday, February 07, 2002 11:10 AM
Subject: [java-list] Problemas ao escrever em arquivos

Pessoal, tenho uma aplicação em Java q grava em um arquivo de log os seus principais eventos. Para isto estou utilizando as classes File e RandomAccessFile, porém estou tendo problemas ao escrever pois, por exemplo, se eu invoco o método:
 
   File infoArq = new File("/home/aryjr/javas/EJB/newsletter/log");
   // Caso o arquivo não exista eu o criarei
   if (!infoArq.exists()) infoArq.createNewFile();
   RandomAccessFile arq = new RandomAccessFile(infoArq,"rw");
   arq.writeUTF("Teste");
   arq.close();
 
Ele escreve a string "Teste" com alguns outros caracteres:
 
"^SÓTeste"
 
Alguém poderia me dar alguma sugestão???
 
Valeu!!!!!

Responder a