【问题标题】:Choosing a fileextension with JFileChooser使用 JFileChooser 选择文件扩展名
【发布时间】:2014-10-12 08:50:16
【问题描述】:

我正在尝试做一些非常基本的事情:

保存文件时,JFileChooser 应该可以选择保存文件的格式,如果没有选择,则默认保存为 .txt。

一件容易的事。 这是我到目前为止所得到的: 一个完全工作的读写类,它接受文件路径和文件名,并创建该文件。 带有File FilterJFileChooser 也已准备就绪,但我只是不知道如何实际使用用户选择的过滤器...

这是选择器的外观。

   chooser = new MyFileChooser(path);   
    FileFilter txtType = new FileNameExtensionFilter("Text File (.txt)", "txt");
    FileFilter htmlType = new FileNameExtensionFilter("HTML File (.HTML)", "HTML");
    chooser.addChoosableFileFilter(txtType);
    chooser.addChoosableFileFilter(htmlType);
    chooser.setFileFilter(txtType);
    chooser.setSelectedFile(new File("Text.txt"));
    int back = chooser.showSaveDialog(null);

    if(back == JFileChooser.APPROVE_OPTION) { 
      path = chooser.getSelectedFile().getPath();
      FileExplorer toWrite= new FileExplorer(path);
      toWrite.writeFile();
    }

如前所述,有两个问题:

-我如何使用所选过滤器中的信息指定文件应具有的扩展名。

-如果在文件名字段中没有声明扩展名并且没有选择过滤器,我如何将 .txt 设置为默认值?

基本上我所要求的只是完成基本或预期的行为。

如果有人可以提供帮助,那就太好了。 谢谢海瑞

【问题讨论】:

    标签: java file filter jfilechooser


    【解决方案1】:

    我对 Stackoverflow 真的很失望......哦,好吧.. 这是修改后的 FileChooser。希望我能帮助别人。 PS:我注意到,将 write 函数打包在 Filechooser 中并不是一个好主意。所以最好的办法是使用 .getFullName(); 获取名称+扩展名;方法并将名称发送到写入函数。

    import java.io.File;
    import javax.swing.Action;
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    
    public class MyFileChooser extends JFileChooser {
    
    public MyFileChooser(String path, String defName) {
        super(path);
        FileFilter txtType = new FileNameExtensionFilter("Text File (.txt)", "txt");
        FileFilter htmlType = new FileNameExtensionFilter("HTML File (.HTML)", "HTML");
        this.addChoosableFileFilter(txtType);
        this.addChoosableFileFilter(htmlType);
        this.setSelectedFile(new File(defName));
        Action details = this.getActionMap().get("viewTypeDetails");
        details.actionPerformed(null);
    }
    
    public String getFullName() {
        String[] temp;
        String extens, temp2, temp3;
    
        temp3 = this.getSelectedFile().getName();
        try {
            temp = this.getFileFilter().getDescription().split("[\\(||\\)]");
    
            if (temp3.endsWith(temp[1])) {
                return temp3;
            }else{
                return temp3 + temp[1];
            }
        }catch (Exception e){
    
            try {
                temp2 = this.getSelectedFile().getName();
                extens = temp2.substring(temp2.lastIndexOf('.')).trim();
    
                return temp3;
            }catch (Exception ee) {
                return temp3 + ".txt";
            }               
        }
    }
    
    
    public String getFolderPath() {
        String inputName = this.getSelectedFile().getName();
        String fullPath = this.getSelectedFile().getPath();
        String result = fullPath.replaceAll(""+ inputName + "$", "");
    
        return result;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 2023-03-19
      • 2017-11-08
      相关资源
      最近更新 更多