JFileChooser(showSaveDialog)无法获取所选扩展文件的值 [英] JFileChooser(showSaveDialog) cant get the value of the extension file chosen

查看:151
本文介绍了JFileChooser(showSaveDialog)无法获取所选扩展文件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个桌面应用程序,它具有JFileChooser(ShowSaveDialog)函数. 当我尝试保存示例文本文件时,程序未获取我选择的扩展文件..我尝试使用if else或switch语句,但无法弄清楚我将使用什么命令来获取字符串/Int选择pdf,word或txt扩展名作为文件扩展名时的条件值...

Im making a desktop application and it has a JFileChooser(ShowSaveDialog) function.. When I tried to save a sample text file the program didnt get the extension file that I chose.. I'm trying to use the if else or switch statement and I cant figure it out what command will I use to get the string/Int value for the condition if pdf,word or txt extension is chosen as file extension...

public class Save {
    static boolean flag = false;
    public static void main(String[] args) throws IOException, SQLException {
        JFileChooser saveFile = new JFileChooser();
        saveFile.setDialogTitle("Save as"); 

        FileNameExtensionFilter File_ext_txt =
            new FileNameExtensionFilter("Text Documents(*.txt)", "txt");
        FileNameExtensionFilter File_ext_pdf =
            new FileNameExtensionFilter("PDF", "pdf");
        FileNameExtensionFilter File_ext_doc =
            new FileNameExtensionFilter("Word 97-2003 Document", "doc");
        saveFile.addChoosableFileFilter(File_ext_pdf);
        saveFile.addChoosableFileFilter(File_ext_doc);
        saveFile.addChoosableFileFilter(File_ext_txt);

        FileFilter extension = saveFile.getFileFilter();
        int userSelection = saveFile.showSaveDialog(null);
        File File_Path = saveFile.getSelectedFile();
        String fullPath = File_Path.getAbsolutePath();
        String Ext = null;
        if (userSelection == JFileChooser.APPROVE_OPTION){
            if(extension == File_ext_txt){
                Ext = "txt";
            }

            File save = new File(fullPath+"."+Ext);
            System.out.println(extension);
            flag = save.createNewFile();
        }
    }
}

推荐答案

我以前遇到过此问题.这是我的程序之一的实用程序功能,您可以使用它代替JFileChooser.getSelectedFile来获取扩展名.

I've encountered this issue before. This is a utility function from one of my programs that you can use instead of JFileChooser.getSelectedFile, to get the extension too.

/**
 * Returns the selected file from a JFileChooser, including the extension from
 * the file filter.
 */
public static File getSelectedFileWithExtension(JFileChooser c) {
    File file = c.getSelectedFile();
    if (c.getFileFilter() instanceof FileNameExtensionFilter) {
        String[] exts = ((FileNameExtensionFilter)c.getFileFilter()).getExtensions();
        String nameLower = file.getName().toLowerCase();
        for (String ext : exts) { // check if it already has a valid extension
            if (nameLower.endsWith('.' + ext.toLowerCase())) {
                return file; // if yes, return as-is
            }
        }
        // if not, append the first extension from the selected filter
        file = new File(file.toString() + '.' + exts[0]);
    }
    return file;
}

这篇关于JFileChooser(showSaveDialog)无法获取所选扩展文件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆