有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要创建一个代码来计算系统中某个文件夹的大小,并在JLabel上显示进度(以KB为单位)。计数部分已经完成。我需要第二部分。它应该通过多线程(每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) 个答案

  1. # 1 楼答案

    这是我自己找到的解决办法。上面的代码正在创建简单的UI。按钮计算给定路径的大小,并打印给定文件夹的大小以及给定路径中的文件和文件夹数

    1. FolderSizeCounter。爪哇

    公共类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实现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。爪哇

    公共类主{

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

    }

  2. # 2 楼答案

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