学习JFilechooser类(9.8)

JFilechooser类是显示文件对话框。

public class ReadFileUsingJFileChooser {
    static String path = "e:\\Users\\ywf\\Desktop";
    public static void main(String[] args) throws IOException{
        JFileChooser filechooser = new JFileChooser(path);//构造一个给定文件路径的文件选择器        
        if(filechooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
            File file = filechooser.getSelectedFile();
            Scanner input = new Scanner(file,"GBK");
            while(input.hasNext()){
                System.out.println(input.next());
            }
            input.close();
        }
        
        
    }

}
View Code

filechooser.showOpenDialog(null)方法显示一个打开文件对话框,返回一个int类型值,或者是APPROVE_OPTION,或者是CANCEL_OPTION,分别表示点击open按钮或者点击cancel按钮。

File file = filechooser.getSelectedFile();返回从文件对话框中选中的文件。

filechooser.showSaveDialog(null);方法同样显示一个保存对话框,返回值同showOpenDialog().

原文地址:https://www.cnblogs.com/yuwenfeng/p/3096462.html