[英]java JFileChooser File Size Filter
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. 例如,一个JFileChooser仅显示3 MB内的图片。
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
从技术上讲,您可以过滤
File
任何属性或属性组合
Create a sub-class of FileFilter. 创建FileFilter的子类。 In the accept method, decide if the file is too large or not.
在accept方法中,确定文件是否太大。
public boolean accept(File f) {
if(f.length() > maxSize) return false;
return true;
}
Then apply the filter to your File Chooser 然后将过滤器应用于您的文件选择器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.