2017-04-07 12 views
0
new Scanner(new URL("SOME_URL.txt").openStream(), "UTF-8").useDelimiter("\\A").next(); 

これを使用して、私はStringに保存する.txt-Fileからデータを取得します。java catch処理時間リアルタイムプログレスバー

私のprogress barについては、そのようなジョブ(または一般的にはメソッドなど)が完了する必要がある時間をカウントすることが可能かどうかを考えました。私はリアルタイムでprocess timeを私のbarに表示することができます。

これはどういうことですか?

enter image description here

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); 
     } 

    } 
+0

私はあなたが何を意味について明確ではないよあなたは、ダウンロードの進行状況を表示しますか、または時間経過? – VGR

+0

ダウンロードの進行状況 – piguy

答えて

3

ことは可能ですか?はい。 Scanner.next()を使用してURLの内容を読み取ることは可能ですか?第

あなたがバイトを自分で読んで、それらをカウントする必要があります「処理時間」

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(); 
関連する問題