Java Swing将文本从JTextArea发送到单独行的txt文件

时间:2018-01-17 00:18:17

标签: java swing jtextarea

我有JTextArea

matrix

当我将此数据写入文本文件时,它会在同一行上打印所有数据:

gribi

如何使文本文件的输出与文本区域中的输出相同,分为两行?

文本区域的代码,并将数据写入文本。

JTextArea matrixArea = new JTextArea();
matrixArea.setBounds(139, 63, 337, 111);
dataPanel.add(matrixArea);

JButton sendData = new JButton("Send Data");
sendData.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String nrPatterns = patternText.getText().toString();
        String inputTex = inputText.getText().toString();
        String otputTex = outputText.getText().toString();
        String matrixW  = matrixArea.getText().toString();

        try {
            FileWriter writer = new FileWriter("test.txt", true);
            writer.write(nrPatterns);
            writer.write(" ");
            writer.write(inputTex);
            writer.write(" ");
            writer.write(otputTex);
            writer.write(" ");
            writer.write(System.getProperty("line.separator"));
            writer.write(matrixW);
            writer.close();
            JOptionPane.showMessageDialog(window, "Success");
        }catch(Exception e){
            JOptionPane.showMessageDialog(window, "Error", "Error",  JOptionPane.ERROR_MESSAGE);
        }
    }
});

1 个答案:

答案 0 :(得分:0)

在最后一次.write后使用:

writer.newLine();