java选择文件
① java中jfilechooser有没有可以选择文件夹的
private JFileChooser fc=new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能选择目录
String path=null;
File f=null;
try{
flag=fc.showOpenDialog(null);
}
catch(HeadlessException head){
System.out.println("Open File Dialog ERROR!");
}
if(flag==JFileChooser.APPROVE_OPTION){
//获得该文件
f=fc.getSelectedFile();
path=f.getPath();
}
//以上获得选择的文件夹
//若要判断其中是否还有其他目录,可以这样做
boolean hasSubDir=false;
File dir=new File(path);
//获得改目录下的文件的文件名,如果没有的话,filesName.length()=0
String filesName=dir.list();
for(int i=0;i<filesName.length();i++){
File temp=new File(path+"/"+filesName[i]);
if(temp.isDirectory()){
hasSubDir=true;
break;
}
}
② Java如何做出好看的本地文件选择器
更改一下外观为windows的:
if(UIManager.getLookAndFeel().isSupportedLookAndFeel()){
final String platform = UIManager.getSystemLookAndFeelClassName();
// If the current Look & Feel does not match the platform Look & Feel,
// change it so it does.
if (!UIManager.getLookAndFeel().getName().equals(platform)) {
try {
UIManager.setLookAndFeel(platform);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
如果不需要文件过滤可以选择 FileDialog来打开仔衡文件,这个是调用操作系统的文件筐野肆打开文件的,但是在window下实现不了文件过滤,其他系统下可以。如果念脊做用JFileChoose可以很容易实现,文件过滤,但是界面默认是java外观。如果要好看,可以设置一下显示外观。