2016-05-22 73 views
0

我需要創建一個代碼來計算系統中某個文件夾的大小並在JLabel上顯示進度(以千字節爲單位)。計數部分完成。我需要第二部分。應該使用多線程來完成(每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

設我谷歌你: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

這是我自己達成的解決方案。上面的代碼創建簡單的用戶界面。按鈕正在計算給定路徑的大小,並正在打印給定文件夾的大小以及給定路徑中的文件和文件夾的數量。

  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
  • 公共class UI im補充(9.3)的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(); 
    } 
    

    }