Problem reading file line by line in Java

Asked

Viewed 296 times

3

Hello, I’m making a java program to convert csv files to Bib. As the csv file may be 200kb or 2G I decided to read line by line so I wouldn’t have memory problems. I did the code as follows:

try {
  File file = new File(caminhoAbrirArquivos + nome);
  Scanner inputStream = new Scanner(file);

  linha = inputStream.nextLine();//A primeira linha é o cabeçalho, então descarto
  while (inputStream.hasNextLine()) {
    linha = inputStream.nextLine();

    //Código que realiza a conversão da linha
  }
} catch (FileNotFoundException ex) {
  JOptionPane.showMessageDialog(null, "Erro: " + ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
}

I finished the program, tested and worked. But when I generated the JAR to use the program it read only the first 30 lines of the file.

When I run the project in Netbeans it reads the entire file and converts, all right, now when I run the project’s JAR and it reads only the first 30 lines of the file and converts only those.

Does anyone know what might be going on?

  • tries to execute the jar via command line. Maybe there is an error

  • I ran it from the command line, there wasn’t even an error and read only the first 30 lines.

  • And the file doesn’t just happen to have 30 lines?

  • kkkkkk... no, it has 198 lines.

  • Can make the file available?

  • Smells like an empty line in the EOF file or tag

  • I put the file in Google Drive: https://drive.google.com/open?id=0B7yryzIu_P8XS1I2T05XMXd6clE

Show 2 more comments

1 answer

1


I was able to solve using the objects Filereader and Bufferedreader, the code went like this:

FileReader arq = new FileReader(caminhoAbrirArquivos + nome);
BufferedReader lerArq = new BufferedReader(arq);

linha = lerArq.readLine();//Primeira linha é o cabeçalho, então é descartada
linha = lerArq.readLine();

while (linha != null) {
    //Código que faz a conversão
}

Browser other questions tagged

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