小弟我在做一个模枋记事本,我做打开和保存这里时,我虽然能实现打开和保存的功能,不过不完美,我打开文件正常,保存文件时由于我是用的FileReader来做的,所以字符流却无法实现文本域里的回车字符,每当向文件里写入时,实现不了换行的功能,等保存完了我点开文件看时全部连在一起,没有换行,而当用打开功能键点开看却是正常的包含得有回车换行,所以我本想用字节来做, 不过总是报错,请大家指教,以下是代码:
package time;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class WordTest extends JFrame implements ActionListener
{
private JMenuBar jmb;//菜单栏;
private JMenu filejm;//文件;
private JMenuItem newjmi;//新建;
private JMenuItem openjmi;//打开;
private JMenuItem savejmi;//保存;
private JMenuItem sasjmi;//另存为;
private JMenuItem exitjmi;//退出;
private JMenu editjm;//编辑;
private JMenuItem pastejmi;//撤消;
private JMenuItem copyjmi;//复制;
private JMenuItem stickjmi;//粘贴;
private JMenuItem deljmi;//删除;
private JMenuItem alljmi;//全选;
private JMenu formatjm;//格式;
private JMenuItem linejmi;//自动换行;
private JMenuItem fontjmi;//字体;
private JMenu helpjm;//帮助;
private JMenuItem colorjmi;//颜色;
private JMenuItem counterjmi;//计算器;
private JMenuItem onjmi;//关于;
private JTextArea jta;//文本域;
private JScrollPane jsp;//滚动条;
private JPanel jtajp;//文件域面板;
private JPanel menujp;//菜单面板;
private JMenuItem red;
private JMenuItem pink;
private JMenuItem yellow;
private JMenuItem green;
private JMenuItem blue;
private FileDialog savefd;//保存对话框;
private FileDialog openfd;//打开对话框;
private FileReader fr;//读文件;
private BufferedReader br;//读缓冲;
private FileWriter fw;//写文件;
private BufferedWriter bw;//写缓冲;
private JFileChooser jfc;
public WordTest()
{
this.setTitle("秋水专用记事本");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
ImageIcon ii = new ImageIcon("{1DDAE082-AA61-4A92-9285-9116FE08DFB9}0.gif.jpg");
this.setIconImage(ii.getImage());
jmb = new JMenuBar();
//jmb.setBorderPainted(true);
filejm = new JMenu("文件(F)");
filejm.setMnemonic('F');
newjmi = new JMenuItem("新建(N)");
newjmi.setMnemonic('N');
newjmi.addActionListener(this);
openjmi = new JMenuItem("打开(O)...");
openjmi.setMnemonic('O');
openjmi.addActionListener(this);
savejmi = new JMenuItem("保存(S)");
savejmi.setMnemonic('S');
savejmi.addActionListener(this);
sasjmi = new JMenuItem("另存为(A)...");
sasjmi.setMnemonic('A');
sasjmi.addActionListener(this);
exitjmi = new JMenuItem("退出(X)");
exitjmi.setMnemonic('X');
exitjmi.addActionListener(this);
filejm.add(newjmi);
filejm.add(openjmi);
filejm.add(savejmi);
filejm.add(sasjmi);
filejm.add(exitjmi);
//将下拉菜单添加到“文件”工具栏;
editjm = new JMenu("编辑(E)");
editjm.setMnemonic('E');
pastejmi = new JMenuItem("撤消(U)");
pastejmi.setMnemonic('U');
pastejmi.addActionListener(this);
copyjmi = new JMenuItem("复制(C)");
copyjmi.setMnemonic('C');
copyjmi.addActionListener(this);
stickjmi = new JMenuItem("粘贴(V)");
stickjmi.setMnemonic('V');
stickjmi.addActionListener(this);
deljmi = new JMenuItem("删除(L)");
deljmi.setMnemonic('L');
deljmi.addActionListener(this);
alljmi = new JMenuItem("全选(A)");
alljmi.setMnemonic('A');
alljmi.addActionListener(this);
editjm.add(pastejmi);
editjm.add(copyjmi);
editjm.add(stickjmi);
editjm.add(deljmi);
editjm.add(alljmi);
//将下拉菜单添加到“编辑”工具栏;
formatjm = new JMenu("格式(O)");
formatjm.setMnemonic('O');
linejmi = new JMenuItem("自动换行(W)");
linejmi.setMnemonic('W');
linejmi.addActionListener(this);
fontjmi = new JMenuItem("字体(F)...");
fontjmi.setMnemonic('F');
fontjmi.addActionListener(this);
formatjm.add(linejmi);
formatjm.add(fontjmi);
//将下拉菜单添加到“格式”工具栏;
helpjm = new JMenu("帮助(H)");
helpjm.setMnemonic('H');
colorjmi = new JMenuItem("更改颜色");
colorjmi.addActionListener(this);
counterjmi = new JMenuItem("计算器(C)");
counterjmi.setMnemonic('C');
counterjmi.addActionListener(this);
onjmi = new JMenuItem("关于(A)");
onjmi.setMnemonic('A');
onjmi.addActionListener(this);
helpjm.add(colorjmi);
helpjm.add(counterjmi);
helpjm.addSeparator();
helpjm.add(onjmi);
//将下拉菜单添加到“关于”工具栏;
jmb.add(filejm);
jmb.add(editjm);
jmb.add(formatjm);
jmb.add(helpjm);
//将工具栏添加到菜单栏中;
menujp = new JPanel();
menujp.add(jmb);
savefd = new FileDialog(this,"保存对话框",FileDialog.SAVE);
savefd.setVisible(false);
openfd = new FileDialog(this,"打开对话框",FileDialog.LOAD);
openfd.setVisible(false);
jfc = new JFileChooser();
jta = new JTextArea(26,66);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jsp = new JScrollPane(jta);
jtajp = new JPanel();
jtajp.add(jsp);
//jta.setBackground(Color.PINK);//北景颜色;
this.getContentPane().add(menujp,BorderLayout.NORTH);
this.getContentPane().add(jtajp,BorderLayout.CENTER);
this.setJMenuBar(jmb);
this.setBounds(0,0,740,543);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args)
{
new WordTest();
}
public void actionPerformed(ActionEvent e)
{
System.out.println("1111");
String str = null;
if(e.getSource().equals(newjmi))
{
//新建还没做,呵。。。
}
else if (e.getSource().equals(openjmi))
{
openfd.setVisible(true);
try {
File readf = new File(openfd.getDirectory(),openfd.getFile());
fr = new FileReader(readf);
br = new BufferedReader(fr);
while((str=br.readLine())!=null)
{
jta.append(str+"\n");
}
br.close();
fr.close();
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
else if(e.getSource().equals(savejmi))
{
savefd.setVisible(true);
try {
File writef = new File(savefd.getDirectory(),savefd.getFile());
fw = new FileWriter(writef);
bw = new BufferedWriter(fw);
bw.write(jta.getText(),0,(jta.getText().length()));
// System.out.println(bw);
bw.flush();
bw.close();
fw.close();
// FileInputStream fis = new FileInputStream(jta.getText());
// DataInputStream dis = new DataInputStream(fis);
// str = dis.toString();
// FileOutputStream fos = new FileOutputStream(str);
// DataOutputStream dos = new DataOutputStream(fos);
// BufferedOutputStream bos = new BufferedOutputStream(dos);
// byte[] buf = new byte[100000];
// bos.write(buf,0,jta.getText().length());
//这里是我准备想用字节来做,不过总是报错,所以我就放弃了这个方法
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
else if(e.getSource().equals(exitjmi))
{
System.exit(0);
}
else if(e.getSource().equals(counterjmi))
{
try {
Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\calc.exe");
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
}