开发者

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

开发者 https://www.devze.com 2022-12-29 21:26 出处:网络
I have a dialog using JFileChooser. When I save a file by FileOutputStream, I want to save it as file.txt in the path which the user want. But it always saves in c:/user/Document.

I have a dialog using JFileChooser. When I save a file by FileOutputStream, I want to save it as file.txt in the path which the user want. But it always saves in c:/user/Document.

Here is the code:

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();
                } catc开发者_StackOverflow社区h (FileNotFoundException fEx) {
                } catch (IOException ioEx) {
                }
            }
        }
        JOptionPane.showMessageDialog(IDRSApplication.idrsJFrame,
            IDRSResourceBundle.res.getString("successful"));
    }

The problem is here: why can't I set path for new file?

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();


The code you provided works as you would expect. (At least under linux.)

I suggest you do a SSCCE and update your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号