将多行JTextArea内容写入文件 [英] Writing multiline JTextArea content into file

查看:144
本文介绍了将多行JTextArea内容写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将textarea的内容写入包含换行符的文件中。我得到了输出,它被写成文件中的一个字符串。

I have to write the content of textarea into a file with line breaks. I got the output like, it is written as one string in the file.

public void actionPerformed(ActionEvent ev) {

text.setVisible(true);
String str= text.getText();
System.out.println(str);

try {
    BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt")); 


    fileOut.write(str);

    fileOut.close();
} catch (IOException ioe) {
    ioe.printStackTrace();
}
}

示例

输出应为:

I
am
King.

但它显示:

IamKing.

请给我一些建议

推荐答案

使用 JTextComponent.write(Writer)


将模型的内容存储到给定的流中。默认情况下,这会将模型存储为纯文本。

Stores the contents of the model into the given stream. By default this will store the model as plain text.



EG



E.G.

BufferedWriter writer = new BufferedWriter(new FileWriter("filename.txt"));
text.write(writer);

这篇关于将多行JTextArea内容写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆