2013-08-22 62 views
1

我有一個按鈕,打開一個保存對話框窗口,默認擴展名過濾器設置,但是當用戶沒有提供文件名的擴展名時,它應該自動添加擴展名。問題是,發生這種情況時文件不會保存(或保存失敗),但不會拋出任何異常。文件保存成功彈出窗口顯示告訴用戶文件已成功保存,但目錄中未找到文件。這裏是我的代碼:當文件名被修改時文件無法保存

private void saveRecordsButtonActionPerformed(java.awt.event.ActionEvent evt)             
    {              
     if(evt.getSource() == this.saveRecordsButton) 
     { 
      String recordName = JOptionPane.showInputDialog(this, "Please type in the name of the record you are about to export: ", "Input Notice", JOptionPane.INFORMATION_MESSAGE); 
      if(recordName == null || recordName.equals("")) 
      { 
       JOptionPane.showMessageDialog(this, "You must type in the name of the record in order to save!", "Input Error!", JOptionPane.ERROR_MESSAGE); 
       return; 
      } 

      int returnVal = this.fileChooser.showSaveDialog(this); 
      if(returnVal == JFileChooser.APPROVE_OPTION) 
      { 
       //ObjectOutput oos = null; 
       try 
       { 
        File file = this.fileChooser.getSelectedFile(); 
        String recordDate = this.viewByDateCB.getSelectedItem().toString(); 
        XMLTableProducer xmlTableProducer = new XMLTableProducer(this.cbtm, "Cash Book Table", recordName, recordDate, new Date()); 

        if(!file.getName().contains(".")) 
        { 
         FileNameExtensionFilter filter = (FileNameExtensionFilter)this.fileChooser.getFileFilter(); 
         file = new File(file.getName()+(filter.getExtensions())[0]); 
         System.out.println(file.getName()); //This actually prints out the exact file name with extension the way I want 
        } 

        // if file doesnt exists, then create it 
        if(!file.exists()) 
        { 
         file.createNewFile(); 
        } 

        FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
        BufferedWriter bw = new BufferedWriter(fw); 
        PrintWriter out = new PrintWriter(bw); 
        out.print(xmlTableProducer.getDynamicText()); 
        out.close(); 
        bw.close(); 

        JOptionPane.showMessageDialog(this, "File Saved Successfully!", "Saved", JOptionPane.INFORMATION_MESSAGE); 
       } 
       catch(IOException xcp) 
       { 
        xcp.printStackTrace(System.err); 
       } 
      } 
     } 
    }   
+1

打印完數據後(並在關閉作家之前),請確保您調用'out.flush();' –

+0

@JoshM假設寫入過程沒有任何問題,不應該「close」沖洗輸出流(個人,我偏執,所以我都這樣做)) – MadProgrammer

回答

2

file = new File(file.getName()+(filter.getExtensions())[0]);被剝離File的路徑...

假設用戶選擇將文件保存在C:\My Documents\Boss中。當你作爲File#getName它只會返回Boss。現在意味着該文件將被保存到

相反file = new File(file.getName()+(filter.getExtensions())[0]);,你應該使用file = new File(file.getPath()+(filter.getExtensions())[0]);其返回由File代表的「完整」路徑和文件名,該方案正在由(即.\Bosss)執行相同的位置

更新...

你的文件寫入過程中也有點過。

一般的經驗法則,如果你打開流,你應該關閉它...

你不應該關閉try-catch內的資源,如果發生Exceptiontry-catch內,該close方法將永遠不會被調用,使資源開...

try 
{ 
    /*...*/ 
    FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
    BufferedWriter bw = new BufferedWriter(fw); 
    PrintWriter out = new PrintWriter(bw); 
    out.print(xmlTableProducer.getDynamicText()); 
    out.close(); 
    bw.close(); 

    JOptionPane.showMessageDialog(this, "File Saved Successfully!", "Saved", JOptionPane.INFORMATION_MESSAGE); 
} 
catch(IOException xcp) 
{ 
    // If an exception occurs, the file will remain open 
    xcp.printStackTrace(System.err); 
} 

相反,你應該使用finally塊嘗試關閉所有的資源,例如...

BufferedWriter bw = null; 
try 
{ 
    /*...*/ 
    FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
    bw = new BufferedWriter(fw); 
    PrintWriter out = new PrintWriter(bw); 
    out.print(xmlTableProducer.getDynamicText()); 

    JOptionPane.showMessageDialog(this, "File Saved Successfully!", "Saved", JOptionPane.INFORMATION_MESSAGE); 
} 
catch(IOException xcp) 
{ 
    xcp.printStackTrace(System.err); 
} finally { 
    try 
    { 
     bw.close(); 
    } 
    catch (Exception exp) { 
    } 
} 
+0

謝謝MadProgrammer,我受到了你的高度教育。 – Jevison7x

+0

這使得一個;) – MadProgrammer

2

代碼看起來沒問題。既然你沒有看到異常,我懷疑你沒有看到正確的目錄。

// if file doesn't exists, then create it 
         if(!file.exists()) 
         { 
          file.createNewFile(); 
         } 

後添加

System.out.println(file.getAbsolutePath()); 

確認你正在尋找到該目錄中有顯示的路徑..

+0

謝謝Zenil,我認爲你已經打開了我的眼睛一點點。它將文件完全保存到所選目錄的不同目錄中。我試圖理解爲什麼會發生這種情況,或者你有什麼想法?所選目錄是「C:\ Users \ Jevison7x \ Documents \」,目標目錄是「C:\ Users \ Jevison7x \ Documents \ NetBeansProjects \ RoyalineApp \」 – Jevison7x

+0

JVM會將其保存到您運行java程序的目錄從。這被稱爲工作目錄。你可以看到它,如果你打印System.getProperty(「user.dir」);在你的情況下,因爲你運行程序RoyalineApp目錄,它將文件保存在那裏..只有當你不指定完整的文件路徑 – Zenil

+0

@ Jevison7x時纔會發生這是我在我的答案中(試圖)解釋的... – MadProgrammer