2010-05-12 120 views
0

我有一個使用JFileChooser的對話框。當我通過FileOutputStream保存文件時,我想將其保存爲用戶需要的路徑中的file.txt。但它總是保存在c:/user/Document如何在JFileChooser中設置新文件的保存路徑?

下面是代碼:

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

重新格式化的代碼;如果不正確請回復。 – trashgod 2010-05-12 14:52:37

+0

感謝您的編輯 – tiendv 2010-05-12 14:57:23

回答

0

您提供的代碼正如您所期望的那樣工作。 (至少在linux下)

我建議你做一個SSCCE並更新你的問題。