开发者

how to open file with specific extension in fileDialogue

I am trying to Open a file with sp开发者_StackOverflowecific extension ( .fcg or .wtg ) using file Dialog is there a way to do it ??


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");
        }
    });


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 {
    }
}


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜