0
I am using this code to read a text file using the RandomAccessFile
, character by character, and generating a string for each word formed, to save to a Hashmap. I need to use the RandomAccessFile
because I need to know the position of the word in the file, so I saved in my Record type this value offset = arq.getFilePointer()
The problem is that when I do the check on while(arq.read() != -1)
the file pointer advances a position, so that I always lose the first letter of my word. If I try to give a arq.seek()
in the previous position, I even picked up the first letter again, but the program is in an infinite loop.
Is there any other way to control when the file reaches the end without using the arq.read()
?
try {
RandomAccessFile arq = new RandomAccessFile("teste.txt", "r");
try{
while(arq.read() != -1) {
String palavra = "";
offset = arq.getFilePointer();
letra = (char)arq.read();
while (letra != ' ' && letra != '\n') {
palavra += letra;
letra = (char)arq.read();
}
palavra = palavra.toLowerCase();
System.out.println(palavra);
if (h.PesquisaRegistro(palavra) == null) {
x = new Registro(palavra, offset);
h.Inserir(x, h.HashCode(x));
} else {
x = h.PesquisaRegistro(palavra);
x.quantidade++;
h.tabela.replace(h.PesquisaChave(palavra), x);
}
}
}catch(EOFException ex){
}
} catch (IOException e) {
}
Thank you very much!!! I had already done so earlier today before I could get in here. It worked!!
– Bruno Camarda