简体   繁体   English

java捕获进程时间实时进度条

[英]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. 使用此^,我从保存在字符串中的.txt文件获取数据。

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. 对于我的progress bar ,我想知道这样的工作(或者通常是方法等)是否有可能计算(或类似地)完成工作的时间。 So that I can show in real time process time in my bar . 这样我就可以在bar实时显示process time

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? 使用Scanner.next()读取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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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