如何在JFileChooser中设置新文件的保存路径?

时间:2010-05-12 14:11:09

标签: java swing

我有一个使用JFileChooser的对话框。当我按FileOutputStream保存文件时,我想将其保存为用户想要的路径中的file.txt。但它始终保存在c:/user/Document中。

以下是代码:

DownLoadDialog downloadDialog = new DownLoadDialog();
int result = downloadDialog.showSaveDialog(queryPanel);
if (result == downloadDialog.APPROVE_OPTION) {

    File file = downloadDialog.getSelectedFile();
    //String parth =file.getPath();
    //System.out.println(parth);
    //if(file.exists()) {
    //int response = JOptionPane.showConfirmDialog (null,
    //    "Overwrite existing file?","Confirm Overwrite",
    //    JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
    //if(response == JOptionPane.OK_OPTION) {}
    //} else {
    if (resultGoogleSearch > 0) {
        {
            String parth = new File(downloadDialog.getSelectedFile().
                getAbsolutePath().concat(".txt")).toString();
            System.out.println(parth);

            for (int i = 0; i < resultGoogleSearch; i++) {
                String[] temp = googleSearchResult.get(i).split("<br>");
                //String resultURL = temp[0];
                //File dir = downloadDialog.getCurrentDirectory();  
                try {
                    FileOutputStream googleReuslt = new FileOutputStream(
                        downloadDialog.getSelectedFile().getAbsolutePath()
                        + ".txt");
                    OutputStreamWriter writer = new
                        OutputStreamWriter(googleReuslt);
                    BufferedWriter buffer = new BufferedWriter(writer);
                    writer.write(temp[0]);
                    writer.close();
                    buffer.close();
                } catch (FileNotFoundException fEx) {
                } catch (IOException ioEx) {
                }
            }
        }
        JOptionPane.showMessageDialog(IDRSApplication.idrsJFrame,
            IDRSResourceBundle.res.getString("successful"));
    }

问题在于:为什么我不能为新文件设置路径?

FileOutputStream googleReuslt = new FileOutputStream(
    downloadDialog.getSelectedFile().getAbsolutePath() + ".txt");
OutputStreamWriter writer = new OutputStreamWriter(googleReuslt);
BufferedWriter buffer = new BufferedWriter(writer);
writer.write(temp[0]);
writer.close();
buffer.close();

1 个答案:

答案 0 :(得分:0)

您提供的代码按预期工作。 (至少在linux下。)

我建议您执行SSCCE并更新您的问题。