JTextArea内容保存到Txt文件,换行问题。

CoreJava 2009-08-20 04:04:44
JTextArea内容保存到Txt文件没有换行。我试过把JTextArea内容的"\n"或"\r"用"\r\n"替换掉还是不行。
关键代码:
JFileChooser fileChooser=new JFileChooser("./");
int result=fileChooser.showSaveDialog(this);
if(result==fileChooser.APPROVE_OPTION){
File file=fileChooser.getSelectedFile();
try {
FileWriter fw=new FileWriter(file);
fw.write(txt.getText());//现在没有把txt.getText
//的"\n"或"\r"用"\r\n"替换。
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
以下是我写的代码,帮我运行一下看看吧。

package mypackage;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class TextTest extends JFrame implements ActionListener{
JTextArea txt=new JTextArea();
JMenuBar menuBar=new JMenuBar();
JMenu[] menu;
JMenuItem[][] menuItem;
Color color=Color.black;

public TextTest(){
this.setTitle("菜单使用");
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
Container content=this.getContentPane();
content.setLayout(new BorderLayout());

String[] menuName={"文件","字体设置"};
String[][] ItemName={{"新建","打开","保存","关闭程序"},{"字体大小","字体颜色"}};

menuBar.setVisible(true);
menu=new JMenu[menuName.length];
for(int i=0;i<menuName.length;i++){
menu[i]=new JMenu(menuName[i]);
menuBar.add(menu[i]);
}
menuItem=new JMenuItem[ItemName.length][];
for(int i=0;i<ItemName.length;i++){
menuItem[i]=new JMenuItem[ItemName[i].length];
for(int j=0;j<ItemName[i].length;j++){
menuItem[i][j]=new JMenuItem(ItemName[i][j]);
if(i==0&&j==3){
menu[i].addSeparator();
}
menuItem[i][j].addActionListener(this);
menu[i].add(menuItem[i][j]);
}
}
this.setJMenuBar(menuBar);
content.add(new JScrollPane(txt),BorderLayout.CENTER);
this.setBounds(200,200,800,500);
}

public void actionPerformed(ActionEvent e){
if(e.getSource()==menuItem[0][0]){
txt.setText("");
}
else if(e.getSource()==menuItem[0][1]){
JFileChooser fileChooser=new JFileChooser("./");
int result=fileChooser.showOpenDialog(this);
if(result==fileChooser.APPROVE_OPTION){
File file=fileChooser.getSelectedFile();
try {
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String str="",s="";
while((str=br.readLine())!=null){
s+=str+"\n";
}
br.close();
fr.close();
txt.setText(s);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else if(e.getSource()==menuItem[0][2]){
JFileChooser fileChooser=new JFileChooser("./");
int result=fileChooser.showSaveDialog(this);
if(result==fileChooser.APPROVE_OPTION){
File file=fileChooser.getSelectedFile();
try {
FileWriter fw=new FileWriter(file);
fw.write(txt.getText());
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else if(e.getSource()==menuItem[0][3]){
this.dispose();
System.exit(0);
}
else if(e.getSource()==menuItem[1][0]){
int n;
String str=JOptionPane.showInputDialog("输入字体大小");
try{
n=Integer.parseInt(str);
txt.setFont(new Font(txt.getFont().getFontName(),Font.PLAIN,n));
}
catch(NumberFormatException m){
JOptionPane.showMessageDialog(this, "输入的信息不是是数字");
m.printStackTrace();
}
}
else if(e.getSource()==menuItem[1][1]){
color=JColorChooser.showDialog(this, "设置字体颜色", color);
txt.setForeground(color);
}
}

}
...全文
678 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
It_EyeIsNotCs_dn 2012-03-22
  • 打赏
  • 举报
回复
博主好定力,09年代帖子,12年给续上了,我过来膜拜的...
CoreJava 2012-03-22
  • 打赏
  • 举报
回复
最后的解决方法:
FileWriter fw=new FileWriter(file);
String str=txt.getText();

for(int i=0;i<str.length();i++){
if(str.charAt(i)==10){
fw.write(13);
fw.write(10);
}
else{
fw.write(str.charAt(i));
}
}
fw.close();
wangqianjiao 2010-04-12
  • 打赏
  • 举报
回复
用newLine()方法与平台无关
huntor 2010-04-12
  • 打赏
  • 举报
回复
怎样读取TextField中的换行符到文本文档中
http://topic.csdn.net/u/20091225/20/31ec4e67-51bb-4705-95e6-454ce6445904.html
Unitimes 2010-04-11
  • 打赏
  • 举报
回复
String lineSeparator = (String) java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
把str = str.replaceAll("\n", lineSeparator)
保存str
monkeyking1987 2009-08-20
  • 打赏
  • 举报
回复
把FileWriter换为PrintWriter,然后调用PrintWriter的println()方法试试,因为这个方法会把数据一行一行地写到文件里去。

62,620

社区成员

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

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