java JFileChooser文件大小过滤器 [英] java JFileChooser File Size Filter

查看:109
本文介绍了java JFileChooser文件大小过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以按文件类型进行过滤,但是可以按文件大小进行过滤吗?

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

例如,一个JFileChooser仅显示3 MB内的图片.

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

推荐答案

简短的答案应该是,您尝试了什么?长答案是肯定的...

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

从技术上讲,您可以过滤File

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

这篇关于java JFileChooser文件大小过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆