JFileChooser保存具有多个文件扩展名的对话框

时间:2015-06-08 12:10:42

标签: java jfilechooser file-extension writer

我正在尝试使用JFileChooserTxtWriter类保存文件,.txt选项和XmlWriter选项保存.xml类。起初我只尝试创建txt选项,但它似乎不起作用。

以下是 JFileChooser 的代码:

JMenuItem mntmCreate = new JMenuItem("Create Report");
mntmCreate.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        switch (chooser.showSaveDialog(frame)){
        case JFileChooser.APPROVE_OPTION:
            File fileToWrite = chooser.getSelectedFile();
            TxtWriter txtwriter = new TxtWriter(taxpayer);
            txtwriter.writeReport(taxpayer);
            System.out.println("Report taxpayer: " +taxpayer.toString());
        }
    }
});

课程 TxtWriter

public class TxtWriter implements Writer {

    public TxtWriter(Taxpayer taxpayer) {
        writeReport(taxpayer);
    }

    public void writeReport(Taxpayer taxpayer) {

        PrintWriter writer;
        try {
            writer = new PrintWriter("Report.txt", "UTF-8");
            printToWriterTaxpayerInfo(taxpayer, writer);
            printToWriterTaxDetails(taxpayer, writer);
            printToWriterReceiptsAmountDetails(taxpayer, writer);
            writer.close();

        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void printToWriterTaxpayerInfo(Taxpayer taxpayer, PrintWriter writer) {
        writer.println("Name: " + taxpayer.getName());
        writer.println("AFM: " + taxpayer.getTaxRegistrationNumber());
        writer.println("Status: " + taxpayer.getStatus());
        writer.println("Income: " + taxpayer.getIncome());
    }

    public void printToWriterTaxDetails(Taxpayer taxpayer, PrintWriter writer) {
        writer.println("Basic Tax: " + taxpayer.getBasicTax());
        writer.println("Tax Increase: " + taxpayer.getTaxIncrease());
        writer.println("Total Tax: "
                + (taxpayer.getBasicTax() + taxpayer.getTaxIncrease()));
    }

    public void printToWriterReceiptsAmountDetails(Taxpayer taxpayer,
            PrintWriter writer) {
        writer.println("Total Receipts Gathered: "
                + taxpayer.computeTotalReceiptsGathered());
        writer.println("Entertainment: "
                + taxpayer.computeReceiptsAmountForKindEntertainment());
        writer.println("Basic: " + taxpayer.computeReceiptsAmountForKindBasic());
        writer.println("Travel: "
                + taxpayer.computeReceiptsAmountForKindTravel());
        writer.println("Health: "
                + taxpayer.computeReceiptsAmountForKindHealth());
        writer.println("Other: " + taxpayer.computeReceiptsAmountForKindOther());
    }
}

同样,XmlWriter文件有一个xml类 我想使用这两个编写器类来创建不同的文件类型。

1 个答案:

答案 0 :(得分:3)

您可以在JFileChooser中添加过滤器,仅允许文本和xml文件。

JFileChooser jf = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
jf.setFileFilter(filter);

或使用addChoosableFileFilter

fc.addChoosableFileFilter(new FileNameExtensionFilter("XMl and text Files", "xml","txt");