我正在尝试使用文件Dialog打开具有特定扩展名(.fcg或.wtg)的文件 有办法吗?
答案 0 :(得分:2)
如果您可以使用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");
}
});
答案 1 :(得分:1)
您可以使用此代码选择文件并阅读
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 {
}
}
答案 2 :(得分:0)
由于这是谷歌搜索#1对我来说,这是最适合我的解决方案:
How to restrict file choosers in java to specific files
细节:
JFileChooser open = new JFileChooser(openPath);
FileNameExtensionFilter filter = new FileNameExtensionFilter("PLSQL Files", "sql", "prc", "fnc", "pks"
, "pkb", "trg", "vw", "tps" , "tpb");
open.setFileFilter(filter);