Update Jprogressbar on how much to read a file

Asked

Viewed 357 times

1

I am developing an application that will perform reading and writing of a particular txt file, which follows a pattern. I will read a file, perform some cleanups on that file, and then write a new file. I put a progress bar to make the process easier for the user. My doubt is to link the time of the reading/writing process, to the progress of the progress bar. I incremented the bar using Thread, but the times don’t match. I was wondering if you have any way to take the exact time of the read/write process, and update the bar. Thank you!

EDITION

My job is this @Guerra. This is the function where I load my file, take out some junk, choose whether I want to break the line or not, and rewrite the file.

    JButton btnIniciar = new JButton("Iniciar");
    btnIniciar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("botão \"iniciar\" precionado...");
            if (txtCaminhoArquivo.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(null, "Selecione um arquivo!");
            } else {
                try {
                    FileInputStream fi = new FileInputStream(arquivo);
                    InputStreamReader input = new InputStreamReader(fi);
                    BufferedReader br = new BufferedReader(input);
                    String linha = br.readLine();

                    OutputStream saida = new FileOutputStream("arquivo_formatado.txt");
                    OutputStreamWriter os = new OutputStreamWriter(saida);
                    BufferedWriter escreva = new BufferedWriter(os);

                    int numeroDeLinhas = 0;
                    do {

                        String novoArquivo[] = linha.split("\t");
                        for (int i = 0; i < novoArquivo.length; i++) {

                            escreva.write(novoArquivo[i] + ";");
                        }

                        // função do chekBox
                        if (chkquebrarLinhas.isSelected()) {
                            escreva.write("\r\n");
                        }

                        linha = br.readLine();
                        numeroDeLinhas++;
                    } while (linha != null);
                    escreva.close();
                    br.close();
                    System.out.println("Arquivo gerado com Sucesso!");

                    System.err.println("Numero de linhas lidas: " + numeroDeLinhas);
                    JOptionPane.showMessageDialog(null, "Arquivo convertido com suceso!");
                } catch (Exception e2) {
                    // TODO: handle exception
                } // fim di catch

            } // fim do else

        }// fim do ActionPeformed

    });// fim do actionListner

As for using the progress bar, end a simple example like this:

new Thread() {
        public void run() {
            for (int i = 0; i < System.currentTimeMillis(); i++) {
                try {
                    sleep(100);
                    barraProgresso.setValue(i);

                    if (barraProgresso.getValue() == 100) {
                        labelBarra.setText("Concluído");
                        JOptionPane.showMessageDialog(null, "Arquivo convertido com sucesso!");
                    }

                } catch (InterruptedException interruptedExepcion) {
                    JOptionPane.showMessageDialog(null, "Erro ao Converter!");
                }
            }
        }
    }.start();

My only question really is how to link process time to bar status.

  • 2

    Show us some code to know how it is and how it should be.

  • OK @Guerra. I will post

No answers

Browser other questions tagged

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