计算文件夹大小并同时显示(多线程Java)

时间:2016-05-22 23:25:18

标签: java multithreading file io

我需要创建一个代码来计算系统中某个文件夹的大小,并在JLabel上显示进度(以千字节为单位)。计数部分完成。我需要第二部分。它应该用多线程完成(每100百万更改一次标签文本)。先感谢您。这是计数代码。

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

2 个答案:

答案 0 :(得分:0)

生成一个单独的线程来调用你的getFolderSize函数,并使foldersize成为一个静态的volatile变量。然后,您可以从UI线程访问foldersize并使用它来定期使用java.util.Timer更新JLabel。

答案 1 :(得分:0)

这是我达成的解决方案。上面的代码是创建简单的UI。按钮正在计算给定路径的大小,并打印给定文件夹的大小以及给定路径中的文件和文件夹的数量。

  1. FolderSizeCounter.java
  2. public class 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];
    }
    

    }

    1. UI.java
    2. 公共类UI实现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);
      
              }
      
          }
      }
      

      }

      1. 和Main.java
      2. public class Main {

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

        }