與\ n

2012-01-04 25 views
0

更換廣場我有一個方法,從一個JTextArea獲取文本並保存在一個txt文件。 這一切工作正常,但文本保存在空空的小方塊代替\ n直線。 我想用\ n來替代這些廣場與 n

我的代碼是:

public void createTxt(){ 

TxtFilter txt = new TxtFilter(); 

JFileChooser fSave = new JFileChooser(); 

fSave.setFileFilter(txt); 
int result = fSave.showSaveDialog(this); 
if(result == JFileChooser.APPROVE_OPTION){ 
    File sFile = new File(fSave.getSelectedFile()+ ".txt"); 

    String file_name = sFile.getName(); 
    String file_path = sFile.getParent(); 

    try{ 
     if(!sFile.exists()){ 

      BufferedWriter out = new BufferedWriter(new FileWriter(sFile)); 

      FileReader fr = new FileReader(jTextArea1.getText()); 
      BufferedReader br = new BufferedReader(fr); 

      String line = ""; 

      while((line = br.readLine())!= null){ 
      line = line.replace("o", "\n"); 
      out.write(line); 
      } 
      out.close(); 

      JOptionPane.showMessageDialog(null, "Warning file • " + file_name + " • created succesfully in \n" + file_path);  
     } 

     else{ 

一定有什麼錯誤的,因爲它創建的txt文件,但它始終是empy,沒有文字是由JTextArea中得到和我總是有例外,因爲從

catch(IOException e){ 
     System.out.println("Error"); 
    } 

錯誤在哪裏?如何修改代碼?

PS我已經「O」寫的,而不是空方,因爲我不知道該寫什麼,以替換第一個字符串(或字符)。

由於

加了:的printStackTrace

java.io.FileNotFoundException: sdfdsf (Impossibile trovare il file specificato) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(FileInputStream.java:106) 
at java.io.FileInputStream.<init>(FileInputStream.java:66) 
at java.io.FileReader.<init>(FileReader.java:41) 
at provegrafica.ProvaFramePop.createTxt(ProvaFramePop.java:154) 
at provegrafica.ProvaFramePop.jMenuSaveActionPerformed(ProvaFramePop.java:129) 
at provegrafica.ProvaFramePop.access$100(ProvaFramePop.java:17) 
at provegrafica.ProvaFramePop$2.actionPerformed(ProvaFramePop.java:64) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
at javax.swing.AbstractButton.doClick(AbstractButton.java:357) 
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809) 
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850) 
at java.awt.Component.processMouseEvent(Component.java:6289) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
at java.awt.Component.processEvent(Component.java:6054) 
at java.awt.Container.processEvent(Container.java:2041) 
at java.awt.Component.dispatchEventImpl(Component.java:4652) 
at java.awt.Container.dispatchEventImpl(Container.java:2099) 
at java.awt.Component.dispatchEvent(Component.java:4482) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
at java.awt.Container.dispatchEventImpl(Container.java:2085) 
at java.awt.Window.dispatchEventImpl(Window.java:2478) 
at java.awt.Component.dispatchEvent(Component.java:4482) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644) 
at java.awt.EventQueue.access$000(EventQueue.java:85) 
at java.awt.EventQueue$1.run(EventQueue.java:603) 
at java.awt.EventQueue$1.run(EventQueue.java:601) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) 
at java.awt.EventQueue$2.run(EventQueue.java:617) 
at java.awt.EventQueue$2.run(EventQueue.java:615) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

生成成功(總時間:19秒),

加了:代碼

else{ 
      String message = "File • " + file_name + " • already exist in \n" + file_path + ":\n" + "Do you want to overwrite?"; 
      String title = "Warning"; 
      int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION); 
      if(reply == JOptionPane.YES_OPTION){ 
       sFile.delete(); 

       BufferedWriter out = new BufferedWriter(new FileWriter(sFile)); 
       out.write(jTextArea1.getText()); 
       out.close(); 
       JOptionPane.showMessageDialog(null, "File • " + file_name + " • overwritten succesfully in \n" + file_path); 

      } 
     } 
+1

嘗試e.printStackTrace( .out.println(「錯誤」),然後將其添加到帖子中。 – 2012-01-04 16:00:42

回答

2

那些的否則部分\ n的。您正在記事本中打開。

你要麼需要打印出每個之前\ n一個\ R(其在這裏可以通過使用緩衝作家,其具有打印線方法簡單地完成),或例如在打開寫字板。

+1

-1,重新發明車輪。 – camickr 2012-01-04 16:06:41

0

更換

line = line.replace("o", "\n"); 

line = line.replaceAll("o", System.getProperty("line.separator")); 
+0

好吧,憤怒的人;) – Vlad 2012-01-04 16:21:15

+0

你們是滑稽與反對投票...即使它不是最優化的解決方案,它也是正確的。所以我沒有看到任何理由來投我的答案。如果你認爲你的答案是更好的留給用戶決定,而不是通過對其他答案進行投票來使其看起來「更好」。 – Vlad 2012-01-05 12:00:31

+1

這是一個初學者的問題。太多初學者,只是複製一個答案而不想。如果回答顯示缺乏關於API的知識,我將低估任何答案。我必須承認這個答案比另一個我低估的答案要好,因爲你至少試圖以OS的格式保存文件。但它仍然是不必要的代碼,並且效率不高。如果他們認爲這是更好的答案,其他用戶可以提高您的答案。因爲我低估了你的答案,所以我得到了滿意的結果。 – camickr 2012-01-05 16:03:18

3

不要推倒重來。

所有文本組件支持write(...)方法。將文本寫入文件時,此方法將使用正確的換行符字符串。所以,你要做的就是:

textArea.write(...); 
0

你在其他部分做 你的程序是無法找到指定的文件。

你在說如果

if(!sFile.exists()){ // don't use not here 
// code to write into the file 
} 

),而不是系統的條件在那裏,因爲它應該是

if(sFile.exists()){ 
// code to write into the file 
} 

使用以下寫入文件

while((line = br.readLine())!= null){ 
     out.append(line); //can also use write in place of append 
     out.append("\r\n"); 
     } 
     out.close();