簡體   English   中英

如何在JFileChooser中設置新文件的保存路徑?

[英]How can I set the save path for new files in JFileChooser?

我有一個使用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();

您提供的代碼可以按預期工作。 (至少在Linux下)。

我建議您進行SSCCE並更新您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM