如何使用JFileChooser 的showSaveDialog()方法--实现保存文件功能?(完整)

来源:互联网 发布:fft变换的c语言实现 编辑:程序博客网 时间:2024/04/29 08:25

                                                 如何使用JFileChooser 的showSaveDialog()方法--实现保存文件功能?
JFileChooser的 showSaveDialog()方法,可以打开一个保存文件的对话框,可具体如何实现-- 保存文件功能呢?

基本思路:打开文件-----获取文件属性[文件路径+文件名]---->使用输入流[InputStream]从磁盘上读取

                    保存文件----->创建文件属性[文件路径+文件名]---->使用输出流[OutPutStream]写到磁盘上

使用 swing  JFileChooser的 showSaveDialog()方法打开文件对话,来保存的话,必须要注意几点:

1、chooser.getSelectedFile(); 这个函数:若是showOpenDialog()对话框,返回的是对话框选中的文件;

如果对话框类型是showSaveDialog的话,那么这里返回的值是你要保存的文件,这个文件可能存在,可能不存在。如果不存在,返回的是--你在对话框中输入的文件名。
既然知道了文件,如果不存在,就新建一个,然后向文件写入数据,就可以实现保存了。另外JFileChooser不会自动帮你读数据并存进去,这些都要自已用代码实现。

2、实现功能完整如下:

其中的open和save是两个JButton,分别实现按钮的功能----打开和保存文件,具体实现代码如下[以匿名类方式注册监听事件]

//....实现--“文件打开”....."文件保存".功能....................................................................    
        open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fileChooser.showOpenDialog(MyMenuEditor.this);           //JFileChooser  fileChooser
                
                BufferedReader br =null;
                contentArea.setText("");       //contentArea是一个编辑文本区JTextArea
                try {
                    br = new BufferedReader(new InputStreamReader(new FileInputStream(fileChooser.getSelectedFile())));
                    while(true){
                        String content = br.readLine();//每次读取一行字符串
                        if(content==null)  break;
                        contentArea.append(content);
                        contentArea.append("\n");//换行
                    }
                
                } catch (FileNotFoundException e1) {
                    JOptionPane.showMessageDialog(MyMenuEditor.this, "文件Not Found-打开失败");
                } catch (IOException e1) {
                    JOptionPane.showMessageDialog(MyMenuEditor.this, "文件读取异常");
                }finally{
                    try {
                        if(br!=null) br.close();
                    } catch (IOException e1) {
                    }
                }
            }
        });

        save.addActionListener(new ActionListener() {
            BufferedWriter bw = null;
            @Override
            public void actionPerformed(ActionEvent e) {
                int select = fileChooser.showSaveDialog(MyMenuEditor.this);
//                fileChooser.setSelectedFile(new File("新建.txt"));
                File file = null;
                
                String fileName = null;
                if(select==JFileChooser.APPROVE_OPTION){
                    file =fileChooser.getSelectedFile();   //如果这里并没有选取中任何的文件,下面的fileChooser.getName(file)将会返回手输入的文件名
                }
                fileName = fileChooser.getName(file);
                if(fileName==null|| fileName.trim().length()==0){
                    JOptionPane.showMessageDialog(MyMenuEditor.this, "文件名为空!");
                }
                
                if(file.isFile()){
                    fileName = file.getName();
                }
                //否则是个文件夹
                file = fileChooser.getCurrentDirectory();//获得当前目录
                
                String path = file.getPath()+java.io.File.separator+fileName;
                file =new File(path);
            
                 if(file.exists()) {  //若选择已有文件----询问是否要覆盖   
                     int i = JOptionPane.showConfirmDialog(MyMenuEditor.this, "该文件已经存在,确定要覆盖吗?");     
                     if(i == JOptionPane.YES_OPTION)   ;     
                     else   return ;    
                     }
                
                
                try {
                    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                    bw.write(contentArea.getText());
                    bw.flush();
                } catch (FileNotFoundException e1) {
                    JOptionPane.showMessageDialog(MyMenuEditor.this, "文件保存出错"+e1.getMessage());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }finally{
                    try {
                        if(bw!=null) bw.close();
                    } catch (IOException e1) {
                    }
                }
            }
        });




1 2
原创粉丝点击