2016-05-22 39 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(); 
    } 
    

    }