做记事本的保存功能时怎么让数据保存的能换行

wuying8624 2009-12-13 04:24:50
我保存为TXT格式后`数据从jTextArea1拿出写入到文件里后`无法自动换行该换行的地方都变成黑块了``````
不知道String 有什么方法能解决这个问题?

JFileChooser chooser=new JFileChooser();

if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
File f=chooser.getSelectedFile();
FileWriter fr=null;
try {
fr=new FileWriter(f);
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter br=new BufferedWriter(fr);

final String all=jTextArea1.getText();//获取jTextArea1里的数据为字符串
//all.replace("", "\r\n"); 想用这个方法替换换行符`但是没有效果`
try {
br.write(all); //把数据写入到文件里
br.flush();
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(all);

}
...全文
234 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwffgqeg 2009-12-13
  • 打赏
  • 举报
回复
mark
zhuyouyong 2009-12-13
  • 打赏
  • 举报
回复
2楼正确
huntor 2009-12-13
  • 打赏
  • 举报
回复

JFileChooser chooser=new JFileChooser();

if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
File f=chooser.getSelectedFile();
Document document = jTextArea1.getDocument();
DefaultEditorKit kit = new DefaultEditorKit();
try {
kit.write(new FileWriter(f), .document, 0,document.getLength());
} catch (Exception e) {
//
}
}
wuying8624 2009-12-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuying8624 的回复:]
别沉了`
[/Quote]
谢了`回去试下`
YangMacgrady 2009-12-13
  • 打赏
  • 举报
回复
学习下,改天也学写这个
yuyeyi 2009-12-13
  • 打赏
  • 举报
回复
楼上正确
jiang5495 2009-12-13
  • 打赏
  • 举报
回复
JtextArea.setLineWrap(true);
调用这个函数就会自动换行了
wuying8624 2009-12-13
  • 打赏
  • 举报
回复
别沉了`

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧