将jTextArea(即另存为)中的文本保存到新的.txt文件中

我正忙着尝试将文字处理器作为我的项目之一,我需要将输入到jTextArea中的文本保存为.txt文件,其中包含用户选择的名称和位置。 注意“fc”是我已经声明的i文件选择器的名称。

public class TextEditor extends javax.swing.JFrame { int count = 2; JTextArea n = new JTextArea(); final JFileChooser fc = new JFileChooser(); public void SaveAs() { final JFileChooser SaveAs = new JFileChooser(); SaveAs.setApproveButtonText("Save"); int actionDialog = SaveAs.showOpenDialog(this); File fileName = new File(SaveAs.getSelectedFile() + ".txt"); try { if (fileName == null) { return; } BufferedWriter outFile = new BufferedWriter(new FileWriter(fileName)); outFile.write(n.getText()); //put in textfile outFile.close(); } catch (IOException ex) { } } 

我会使用JTetArea自己的write方法,因为这样可以轻松写入文件并很好地处理所有换行。 例如(并借用你的代码):

 public class TextEditor extends JFrame { int count = 2; JTextArea n = new JTextArea(); final JFileChooser fc = new JFileChooser(); public void SaveAs() { final JFileChooser SaveAs = new JFileChooser(); SaveAs.setApproveButtonText("Save"); int actionDialog = SaveAs.showOpenDialog(this); if (actionDialog != JFileChooser.APPROVE_OPTION) { return; } File fileName = new File(SaveAs.getSelectedFile() + ".txt"); BufferedWriter outFile = null; try { outFile = new BufferedWriter(new FileWriter(fileName)); n.write(outFile); // *** here: *** } catch (IOException ex) { ex.printStackTrace(); } finally { if (outFile != null) { try { outFile.close(); } catch (IOException e) { // one of the few times that I think that it's OK // to leave this blank } } } } } 

您的代码中有一些错误。 对于例如这项工作,

 import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.io.*; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; @SuppressWarnings("serial") public class TextEditor extends JFrame { int count = 2; JTextArea textArea = new JTextArea(10, 30); final JFileChooser fc = new JFileChooser(); public TextEditor() { add(new JScrollPane(textArea)); add(new JPanel(){{add(new JButton(new AbstractAction("Save As") { @Override public void actionPerformed(ActionEvent arg0) { saveAs(); } }));}}, BorderLayout.SOUTH); } public void saveAs() { FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("Text File", "txt"); final JFileChooser saveAsFileChooser = new JFileChooser(); saveAsFileChooser.setApproveButtonText("Save"); saveAsFileChooser.setFileFilter(extensionFilter); int actionDialog = saveAsFileChooser.showOpenDialog(this); if (actionDialog != JFileChooser.APPROVE_OPTION) { return; } // !! File fileName = new File(SaveAs.getSelectedFile() + ".txt"); File file = saveAsFileChooser.getSelectedFile(); if (!file.getName().endsWith(".txt")) { file = new File(file.getAbsolutePath() + ".txt"); } BufferedWriter outFile = null; try { outFile = new BufferedWriter(new FileWriter(file)); textArea.write(outFile); } catch (IOException ex) { ex.printStackTrace(); } finally { if (outFile != null) { try { outFile.close(); } catch (IOException e) {} } } } private static void createAndShowGui() { TextEditor frame = new TextEditor(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } 

您似乎(虽然您的某些代码缺失)使用FileReader从所选文件中读取,然后使用FileWriter写入同一文件。 显然,这是围绕着圈子。

您需要调用JTextArea方法( getText()等)来获取文本,然后将其写入文件。

这是什么this.n

另请注意,您正在使用catch (IOException ex) {}静默捕获exception,即不记录任何错误 – 因此如果出现问题,您将无法获得任何信息。

最后,你应该使用finally来关闭你的文件 – 如果你在try块中执行它,如果有exception,它将不会被关闭。

更新 (现在Q已被编辑):大概你的JFileChooser正在返回一个目录。 然后你将“.txt”附加到它。 我不认为这就是你的意思。 尝试在写入之前打印出fileName 。 请在写之前打印出n.getText() ,然后告诉我们你的看法。 还请将println放入teh catch块中,以便确认是否抛出exception。

你只需要在最后关闭你的文件,所以它会写入文本。

例:

 BufferedWriter wr; try { wr = new BufferedWriter(new FileWriter(path)); wr.write(edytor.getText()); wr.close(); } catch (IOException ex) { Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex); }