Как установить путь сохранения для новых файлов в JFileChooser? - PullRequest
0 голосов
/ 12 мая 2010

У меня есть диалог с использованием 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 голосов
/ 12 мая 2010

Код, который вы указали, работает так, как вы ожидаете. (По крайней мере, под Linux.)

Я предлагаю вам сделать SSCCE и обновить ваш вопрос.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...