Problem with inifnito loop Randomaccessfile

Asked

Viewed 60 times

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) {
}

2 answers

0


You could set a variable before the while and store the return of the first read in it.


With while:

RandomAccessFile arq = new RandomAccessFile("teste.txt", "r");
int letraByte = arq.read();
while (letraByte != -1){
    String palavra = "";

    while ((char) letraByte != ' ' && (char) letraByte != '\n' && letraByte != -1) {
        palavra += (char) letraByte;
        letraByte = arq.read();
    }

    letraByte = arq.read();
    System.out.println(palavra);
}

With while:

RandomAccessFile arq = new RandomAccessFile("teste.txt", "r");
int letraByte;
do {
    String palavra = "";
    letraByte = arq.read();

    while ((char) letraByte != ' ' && (char) letraByte != '\n' && letraByte != -1) {
        palavra += (char) letraByte;
        letraByte = arq.read();
    }
    System.out.println(palavra);
} while (letraByte != -1);
  • Thank you very much!!! I had already done so earlier today before I could get in here. It worked!!

0

You can simply call Arq.read() before and at the end of while and put the value into a variable and test this variable. So you can read the value after without walking the file pointer.

  • I don’t know if I understood what you meant, but come on, you said to call the arq.read() before the while. Okay. Then I’ve lost the first letter of the word. If I call once at the end too, I lose another letter. If I save in a variable, every time I save, I will end up "losing" a character. I’m sorry, I didn’t really understand.

  • It’s because I’m on my cell phone right now, and it gets hard to show in code. But you will not miss if you always call Arq.read() you put the value read in this variable. You’ll have to call once before the while to have something to compare the first time, and then call at the end of the while to read the next loop.

  • well, if I understand it right, it’s doing what I’m doing with letra. I’m saving the value of arq.read() at each iteration, and use this value as a comparison?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.