2016-05-22 6 views
0

システムの一部のフォルダのサイズを数え、進捗状況をJLabelに表示するコードを作成する必要があります(キロバイト単位)。カウント部が終了します。私は2番目の部分が必要です。それは、100ミリメートルごとにラベルテキストを変更するマルチスレッドで行う必要があります。前もって感謝します。ここにカウントコードがあります。フォルダのサイズをカウントして同時に表示する(マルチスレッドJava)

public static long getFileSize(File folder) { 

    long foldersize = 0; 

    File[] filelist = folder.listFiles(); 
    for (int i = 0; i < filelist.length; i++) { 

     if (filelist[i].isDirectory()) { 

      foldersize += getFileSize(filelist[i]); 

     } else { 

      foldersize += filelist[i].length(); 

     } 

    } 

    return foldersize; 
} 
+0

レッツ私はあなたのためにgoogle:https://docs.oracle.com/javase/tutorial/uiswing/ components/progress.html#bars、https://docs.oracle.com/javase/tutorial/uiswing/examples/components/ProgressBarDemoProject/src/components/ProgressBarDemo.java – Yev

+0

使用するJLabelはどこにありますか?これまでに何を試しましたか?あなたのコードを書くことを期待していますか? – UDKOX

+0

ありがとう、私は自分自身の解決策を見つけました。答えに投稿してください、多分誰かがそれを使うでしょう。 –

答えて

0

スポーン別のスレッドあなたgetFolderSize関数を呼び出し、そしてfoldersize静的volatile変数を作成します。次に、あなたのUIスレッドからfoldersizeにアクセスし、それを使ってjava.util.Timerを使って定期的にJLabelを更新することができます。

0

私は自分自身に到達した解決策です。上のコードは簡単なUIを作成しています。ボタンは、指定されたパスのサイズをカウントしており、指定されたフォルダのサイズと、指定されたパスのファイルとフォルダの数を表示します。

  1. FolderSizeCounter.java

パブリッククラスFileSizeCounter {

public static long getFileSize(File folder){ 


    long foldersize = 0; 
    File[] filelist = folder.listFiles(); 
    if(filelist!=null){ 
     for (int i=0; i < filelist.length; i++) { 

      if(!filelist[i].isDirectory() && !filelist[i].isFile()){ 

       UI.nonfilecount++; 
      } 
      if (filelist[i].isDirectory()) { 

       foldersize += getFileSize(filelist[i]); 
       UI.foldercount++; 

      } else { 
       foldersize += filelist[i].length(); 
       UI.size+=filelist[i].length(); 
       UI.filecount++; 
      } 

     } 

    } 

    return foldersize; 
} 

public static String getReadableSizeByte(long size) { 
    if(size <= 0) return "0"; 
    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; 
    int digitGroups = (int) (Math.log10(size)/Math.log10(1024)); 
    return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) 
      + " " + units[digitGroups]; 
} 

public static String getReadableSizeK(long size) { 
    if(size <= 0) return "0"; 
    final String[] units = new String[] { " ","K", "M", "B", "T"}; 
    int digitGroups = (int) (Math.log10(size)/Math.log10(1000)); 
    return new DecimalFormat("#,##0.#").format(size/Math.pow(1000, digitGroups)) 
      + " " + units[digitGroups]; 
} 

}

  • UI.java
  • 公衆クラスUI im plementsのActionListener {

    private JFrame fr; 
    private JPanel pn; 
    static JLabel folderSize, info, folderCountlbl, fileCountlbl; 
    private JTextField tf; 
    private JButton but; 
    public static long size, foldercount, filecount, nonfilecount; 
    public int i = 0; 
    
    public UI() { 
    
        fr = new JFrame(); 
    
        pn = new JPanel(); 
    
        info = new JLabel("Type the folder path !!!"); 
        folderSize = new JLabel(); 
        folderCountlbl = new JLabel(); 
        fileCountlbl = new JLabel(); 
    
        tf = new JTextField(12); 
    
        but = new JButton("Count the size !!!"); 
    
        pn.add(info); 
        pn.add(tf); 
        pn.add(but); 
        pn.add(folderSize); 
        pn.add(folderCountlbl); 
        pn.add(fileCountlbl); 
    
        but.addActionListener(this); 
    
        fr.add(pn); 
    
        fr.setSize(220, 180); 
        fr.setLocationRelativeTo(null); 
        fr.setResizable(false); 
        fr.setVisible(true); 
        fr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    
    } 
    
    @Override 
    public void actionPerformed(ActionEvent e) { 
    
        if (e.getSource() == but) { 
         if (new File(tf.getText()).isDirectory()) { 
    
          Thread t = new Thread(new Runnable() { 
    
           @Override 
           public void run() { 
    
            System.out.println(FileSizeCounter 
              .getReadableSizeByte(FileSizeCounter.getFileSize(new File(tf.getText())))); 
    
           } 
          }); 
    
          Thread t1 = new Thread(new Runnable() { 
    
           @Override 
           public void run() { 
            while (t.isAlive()) { 
    
             folderSize.setText("Size of the folder " + FileSizeCounter.getReadableSizeByte((size))); 
             folderCountlbl 
               .setText("Number of folders " + FileSizeCounter.getReadableSizeK(foldercount)); 
             fileCountlbl.setText("Number of files " + FileSizeCounter.getReadableSizeK(filecount)); 
             try { 
              Thread.sleep(1); 
             } catch (InterruptedException e) { 
              // TODO Auto-generated catch block 
              e.printStackTrace(); 
             } 
    
            } 
            folderSize.setText("Size of the folder " + FileSizeCounter.getReadableSizeByte((size))); 
            folderCountlbl.setText("Number of folders " + FileSizeCounter.getReadableSizeK(foldercount)); 
            fileCountlbl.setText("Number of files " + FileSizeCounter.getReadableSizeK(filecount)); 
           } 
          }); 
          t1.start(); 
          t.start(); 
         } 
    
         else { 
          JOptionPane.showMessageDialog(fr, "Wrong path. Try again.", "ERROR", JOptionPane.ERROR_MESSAGE); 
          System.exit(0); 
    
         } 
    
        } 
    } 
    

    }

  • とMain.java
  • パブリッククラスメイン{

    public static void main(String args[]) { 
    
        new UI(); 
    } 
    

    }

    関連する問題