简体   繁体   中英

java catch process time real time progress bar

new Scanner(new URL("SOME_URL.txt").openStream(), "UTF-8").useDelimiter("\\A").next();

Using this ^ I get the data from a .txt-File which I save in a String.

For my progress bar , I wondered if it would be possible for jobs like that (or in general for methods etc.) to count (or sth. like that) the time it needed to be finished. So that I can show in real time process time in my bar .

Is this somehow possible?

在此处输入图片说明

EDIT:

package app.gui;

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.net.URL;
    import java.util.Scanner;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;

    import org.apache.commons.io.IOUtils;

    public class Updater {

        private JFrame frame;
        private static String rawG;
        private static String versI;

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        rawG = new Scanner(new URL("SOME_URL.txt").openStream(), "UTF-8").useDelimiter("\\A").next();
                        versI = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("version.txt"));
                    } catch (Exception e) {
                        System.out.println("error class Updater try/catch raw github");
                    }
                    if (Integer.parseInt(rawG.split("\\.")[0]) < Integer.parseInt(versI.split("\\.")[0])) {
                        System.out.println("Version check failure, update needed");
                        try {
                            Updater window = new Updater();
                            window.frame.setVisible(true);
                        } catch (Exception e) {
                            System.out.println("error class Updater try/catch initialize frame");
                        }                   
                    } else {
                        System.out.println("Version check correct, no update needed");
                    }
                }
            });
        }

        public Updater() {
            initialize();
        }

        private void initialize() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                    | UnsupportedLookAndFeelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            frame.getContentPane().add(panel, BorderLayout.SOUTH);
            panel.setLayout(new BorderLayout(0, 0));

            JProgressBar progressBar = new JProgressBar();
            progressBar.setStringPainted(true);
            panel.add(progressBar, BorderLayout.NORTH);
        }

    }

Is it possible? Yes. Is it possible when using Scanner.next() to read the contents of the URL? No.

You will need to read the bytes yourself, and count them:

URL url = new URL("SOME_URL.txt");
URLConnection conn = url.openConnection();

ByteBuffer buffer = ByteBuffer.allocate(conn.getContentLength());
EventQueue.invokeLater(() -> progressBar.setMaximum(buffer.limit()));
EventQueue.invokeLater(() -> progressBar.setValue(0));
try (ReadableByteChannel channel = Channels.newChannel(conn.getInputStream())) {
    while (channel.read(buffer) >= 0) {
        EventQueue.invokeLater(() -> progressBar.setValue(buffer.position()));
    }
}

buffer.flip();
String rawG = StandardCharsets.UTF_8.decode(buffer).toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM