java JFileChooser File Size Filter

前端 未结 2 1799
后悔当初
后悔当初 2021-01-16 12:53

I know I can make a filter by file type, but is it possible to filter by file size?

For example a JFileChooser to show only pictures within 3 MegaBytes.

2条回答
  •  醉酒成梦
    2021-01-16 12:57

    The short answer should be, what have you tried? The long answer is yes...

    JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(new FileFilter() {
    
        @Override
        public boolean accept(File f) {
            String name = f.getName().toLowerCase();
            return (name.endsWith(".png") &&
                            name.endsWith(".jpg") &&
                            name.endsWith(".gif") &&
                            name.endsWith(".bmp") &&
                            f.length() < 3 * (1024 * 1024));
        }
    
        @Override
        public String getDescription() {
            return "Images < 3mb";
        }
    });
    

    Technically, you can filter on any property or combination of properties from File

提交回复
热议问题