62,621
社区成员
发帖
与我相关
我的任务
分享
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);
}
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) {
//
}
}