2012-11-18 32 views
0

可能重複:
Writing multiline JTextArea content into file文件保存爲文本區域的概率

我已經寫代碼,保存文件從我的文本區域的問題是,它可以節省這一切在在新的文本文件一行,而不是在我的文字box.below是我的代碼

String text = dna_ta.getText(); 

    JFileChooser chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new File("./")); 
    int actionDialog = chooser.showSaveDialog(this); 
    if (actionDialog == JFileChooser.APPROVE_OPTION) 
    { 
     BufferedWriter out = null; 
     try { 
      File fileName = new File(chooser.getSelectedFile() + ""); 
      if(fileName == null) 
       return; 
      if(fileName.exists()) 
      { 
       actionDialog = JOptionPane.showConfirmDialog(this, 
            "Replace existing file?"); 
       if (actionDialog == JOptionPane.NO_OPTION) 
        return; 
      } 
      out = new BufferedWriter(new FileWriter(fileName)); 
      out.write(text); 
      out.close(); 
在我的T

內線方面是如下

asd 
aaaaaa 

但在我的文本文件保存它的如下

asdaaaaaa 

我無法揣摩出我已經錯將其保存爲相同的格式,在我textarea.thank你

+0

請注意,我刪除了[tag:netbeans]標記並添加了[tag:swing]標記,因爲此問題與NetBeans無關,並且都與Swing和IO有關。 –

回答

5

你可以只用JTextArea中的write(...)方法來爲你做這個:

out = new BufferedWriter(new FileWriter(fileName)); 
dna_ta.write(out); 
out.close(); // after first checking if null 

從JTextComponent派生的所有組件(包括JTextArea)都具有此方法,該方法將使用特定於OS的新行字符來編寫新行。

+0

感謝隊友的回答 –

+0

@Prity:不客氣 –