Update JProgressBar on how much to read a file

1

I'm developing an application that will perform reading and writing of a particular txt file, which follows a pattern. I'm going to read a file, perform some cleanup on that file, and then write a new file. I put a progress bar to make it easier for the user to process. My question is to link the time of the read / write process when the progress bar progresses. I've incremented the bar using Thread, but times do not match. I was wondering if you have any way to get the exact time of the read / write process, and update the bar. Thanks!

EDITION

My role is this @Guerra. This is the function where I load my file, throw some junk, choose whether 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 real question is how to link process time to bar status.

    
asked by anonymous 16.12.2015 / 21:39

0 answers