如何在fileDialogue中打开具有特定扩展名的文件

发布于 2024-11-06 04:21:29 字数 54 浏览 1 评论 0原文

我正在尝试使用文件对话框打开具有特定扩展名( .fcg 或 .wtg )的文件 有办法做到吗?

I am trying to Open a file with specific extension ( .fcg or .wtg ) using file Dialog
is there a way to do it ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短暂陪伴 2024-11-13 04:21:29

如果您可以使用JFileChooser,则可以使用JFileChooser.addChoosableFileFilter()按扩展名过滤文件。

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "*.fct, *.wtg";
        }

        @Override
        public boolean accept(File f) {
            return f.getName().endsWith(".fcg") ||
            f.getName().endsWith(".wtg");
        }
    });

If you can use JFileChooser you can use JFileChooser.addChoosableFileFilter() to filter files by extension.

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "*.fct, *.wtg";
        }

        @Override
        public boolean accept(File f) {
            return f.getName().endsWith(".fcg") ||
            f.getName().endsWith(".wtg");
        }
    });
心的憧憬 2024-11-13 04:21:29

您可以使用此代码选择一个文件并读取

public void openFile() throws Exception {
    int rowCount = 0;
    int rowNo = 2;
    String id = "";
    String name = "";
    JFileChooser fc = new JFileChooser();
    int result = fc.showOpenDialog(new JPanel());
    if (result == JFileChooser.APPROVE_OPTION) {
        String file = String.valueOf(fc.getSelectedFile());

        File fil = new File(file);
        System.out.println("File Selected" + file);
        FileInputStream fin = new FileInputStream(fil);
        int ch;
        while ((ch = fin.read()) != -1) {
            System.out.print((char)ch);
        }
    } else {
    }
}

You can use this this code to choose a file and read

public void openFile() throws Exception {
    int rowCount = 0;
    int rowNo = 2;
    String id = "";
    String name = "";
    JFileChooser fc = new JFileChooser();
    int result = fc.showOpenDialog(new JPanel());
    if (result == JFileChooser.APPROVE_OPTION) {
        String file = String.valueOf(fc.getSelectedFile());

        File fil = new File(file);
        System.out.println("File Selected" + file);
        FileInputStream fin = new FileInputStream(fil);
        int ch;
        while ((ch = fin.read()) != -1) {
            System.out.print((char)ch);
        }
    } else {
    }
}
娇纵 2024-11-13 04:21:29

因为这对我来说是谷歌搜索#1,所以这是最适合我的解决方案:

如何将java中的文件选择器限制为特定文件

细节:

    JFileChooser open = new JFileChooser(openPath);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PLSQL Files", "sql", "prc", "fnc", "pks"
                                                                    , "pkb", "trg", "vw", "tps" , "tpb");
    open.setFileFilter(filter);

As this is google search #1 for me, this is the solution which worked best for me:

How to restrict file choosers in java to specific files

specifics:

    JFileChooser open = new JFileChooser(openPath);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PLSQL Files", "sql", "prc", "fnc", "pks"
                                                                    , "pkb", "trg", "vw", "tps" , "tpb");
    open.setFileFilter(filter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文