用JAVA中写记事本,如何实现保存和打开文件

还有如何剪切、复制和粘贴功能?最好有详细的源码和注释!谢谢了!满意后再给20分2楼lanhawk的答案很不错,对我来说很有用!也谢谢其他两位给的答案!lanhawk:你写... 还有如何剪切、复制和粘贴功能?
最好有详细的源码和注释!谢谢了!

满意后再给20分
2楼lanhawk 的答案很不错,对我来说很有用!也谢谢其他两位给的答案!
lanhawk :你写的保存方法存不上,总报异常
还有,在保存的时候,要判断这个文件是否为第一次保存
这个判断怎么来写?
谢谢
展开
 我来答
lanhawk
2007-01-26 · TA获得超过834个赞
知道小有建树答主
回答量:267
采纳率:0%
帮助的人:328万
展开全部
JTextArea 里面有些方法,能实现.
txtEdit.copy();复制
txtEdit.paste();粘贴
txtEdit.cut();剪切
部分代码:
--------------------------------
JTextArea txtEdit = new JTextArea();
JMenuBar mb = new JMenuBar();

JMenu menuEdit = new JMenu("编辑(E)");

JMenuItem menuCut = new JMenuItem("剪切(T)");
JMenuItem menuCopy = new JMenuItem("复制(C)");
JMenuItem menuPaste = new JMenuItem("粘贴(P)");
JMenuItem menuDel = new JMenuItem("删除(L)");

构造方法中:
menuEdit.add(menuUndo);
menuEdit.addSeparator();
menuEdit.add(menuCut);
menuEdit.add(menuCopy);
menuEdit.add(menuPaste);
menuEdit.add(menuDel);

//加入快捷键
menuCut.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.CTRL_MASK));
menuCopy.setAccelerator(KeyStroke.getKeyStroke('C',InputEvent.CTRL_MASK));
menuPaste.setAccelerator(KeyStroke.getKeyStroke('V',InputEvent.CTRL_MASK));

//加入事件监听
menuCut.addActionListener(this);
menuCopy.addActionListener(this);
menuPaste.addActionListener(this);

方法:
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser(".");
if(e.getSource().equals(menuCopy))//复制
{
txtEdit.copy();
}
else if(e.getSource().equals(menuPaste))//粘贴
{
txtEdit.paste();
}
else if(e.getSource().equals(menuCut))//剪切
{
txtEdit.cut();
}

----------------
//保存和读取,I/O流操作.我只写了方法

public void readFile()//用于读取文件内容
{
try
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null)
{
txtEdit.setText(txtEdit.getText()+str+"\n");
}
br.close();
fr.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public void saveFile()//保存文件
{
try
{
FileWriter fw = new FileWriter(file);
fw.write(txtEdit.getText());
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
----------------
剩下的你自己实现吧...
------------------------------
在public void actionPerformed(ActionEvent e)中
if(e.getSource().equals(menuOpen))//打开
{
int result = fc.showOpenDialog(this);//显示打开对话框
if(result == 0)//点击打开
{
file = fc.getSelectedFile();
this.setTitle(file.getName()+" - 记事本");
readFile();
}
}

if(e.getSource().equals(menuSave) || e.getSource().equals(menuSaveAs))//保存
{
int result = fc.showSaveDialog(this);//显示保存对话框
if(result == 0)//点击了保存
{
file = fc.getSelectedFile();
this.setTitle(file.getName() + " - 记事本");
saveFile();
}
}
-----------------
代码经过我测试,没有什么异常...
你自己再仔细看看吧...
nokia1112
2007-01-25 · TA获得超过138个赞
知道小有建树答主
回答量:184
采纳率:0%
帮助的人:0
展开全部
/**一个类似于视窗系统里记事本的程序
* 可以对文本文件进行简单的编辑和浏览
*/
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class TextEditor {
char buffer[]=new char[1024];
String sou="";
String des="";
long souTime,desTime;
JFrame mainWindow=new JFrame("文本编辑器");
JMenuBar menuBar=new JMenuBar();
JMenu fileMenu=new JMenu("文件");
JMenu helpMenu=new JMenu("帮助");
JMenuItem openItem=new JMenuItem("打开");
JMenuItem saveItem=new JMenuItem("保存");
JMenuItem resaveItem=new JMenuItem("另存为");
JMenuItem exitItem=new JMenuItem("退出");
JMenuItem helpItem=new JMenuItem("关于文本编辑器");
JTextArea textArea=new JTextArea(15,30);
JScrollPane js = new JScrollPane();
JLabel labelInfo=new JLabel("状态栏");
Listener listen=new Listener();
JFileChooser openDialog=new JFileChooser("F:\\");
//窗体初始化方法
public void init(){
//把菜单项加入到菜单栏中
menuBar.add(fileMenu);
menuBar.add(helpMenu);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(resaveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
helpMenu.add(helpItem);
//给菜单组件添加监听器
openItem.addActionListener(listen);
saveItem.addActionListener(listen);
resaveItem.addActionListener(listen);
exitItem.addActionListener(listen);
helpItem.addActionListener(listen);
js.getViewport().setView(textArea);
//添加窗体中的组件
mainWindow.setJMenuBar(menuBar);
mainWindow.getContentPane().add(js,BorderLayout.CENTER);
mainWindow.getContentPane().add(labelInfo,BorderLayout.SOUTH);
//mainWindow.setBounds(0, 0, 300, 300);
mainWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//窗口监听事件,当关闭窗口时触发
mainWindow.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//System.exit(0);
if(sou.equals("")){
int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");
if(len==JOptionPane.NO_OPTION) System.exit(0);
else{
if(len==JOptionPane.YES_OPTION){
len=openDialog.showSaveDialog(mainWindow);
if(len==JFileChooser.APPROVE_OPTION){
File exitFile=openDialog.getSelectedFile();
des=textArea.getText();
save(exitFile);
sou=des;
if(exitFile.exists()) labelInfo.setText(exitFile.getPath()+"保存成功");
System.exit(0);
}
else labelInfo.setText("返回原程序");
}
else labelInfo.setText("返回原程序");
}
}
else{
des=textArea.getText();
labelInfo.setText("退出程序");
if(sou.equals(des)==false){
int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");
if(len==JOptionPane.NO_OPTION) System.exit(0);
else{
if(len==JOptionPane.YES_OPTION){
len=len=openDialog.showSaveDialog(mainWindow);
File exitFile=openDialog.getSelectedFile();
des=textArea.getText();
save(exitFile);
if(exitFile.exists()) labelInfo.setText(exitFile.getPath()+"保存成功");
}
else labelInfo.setText("返回原程序");
}
}
else System.exit(0);
}
}
});
//按适合的大小显示窗体于屏幕中央
mainWindow.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainWindow.setSize(300,200);
Dimension frameSize = mainWindow.getPreferredSize();
if(frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}
if(frameSize.width > screenSize.width){
frameSize.width = screenSize.width;
}
mainWindow.setLocation(( (screenSize.width - frameSize.width) / 2),
( (screenSize.height - frameSize.height) / 2));
mainWindow.setSize(frameSize);
mainWindow.setVisible(true);
}
//菜单组件监听器类
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
//“打开”菜单项处理动作
if(command.equals("打开")){
int len=openDialog.showOpenDialog(mainWindow);
File openFile=openDialog.getSelectedFile();
if(len==JFileChooser.APPROVE_OPTION){
open(openFile);
souTime=openFile.lastModified();
}
}
//“保存”菜单项处理动作
if(command.equals("保存")){
if(sou.equals("")){
int len=openDialog.showSaveDialog(mainWindow);
if(len==JFileChooser.APPROVE_OPTION){
File resaveFile=openDialog.getSelectedFile();
des=textArea.getText();
save(resaveFile);
sou=des;
if(resaveFile.exists()) labelInfo.setText(resaveFile.getPath()+"保存成功");
}
}
else{
File saveFile=openDialog.getSelectedFile();
des=textArea.getText();
if(sou.equals(des)==false){
save(saveFile);
sou=des;}}
}
//“另存为”菜单项处理动作
if(command.equals("另存为")){
int len=openDialog.showSaveDialog(mainWindow);
if(len==JFileChooser.APPROVE_OPTION){
File resaveFile=openDialog.getSelectedFile();
des=textArea.getText();
save(resaveFile);
if(resaveFile.exists()) labelInfo.setText(resaveFile.getPath()+"保存成功");
}
else labelInfo.setText("返回原程序");
}
//“退出”菜单项处理动作
if(command.equals("退出")){
if(sou.equals("")){
int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");
if(len==JOptionPane.NO_OPTION) System.exit(0);
else{
if(len==JOptionPane.YES_OPTION){
len=openDialog.showSaveDialog(mainWindow);
if(len==JFileChooser.APPROVE_OPTION){
File exitFile=openDialog.getSelectedFile();
des=textArea.getText();
save(exitFile);
sou=des;
if(exitFile.exists()) {
labelInfo.setText(exitFile.getPath()+"保存成功");
}
System.exit(0);
}
else labelInfo.setText("返回原程序");
}
else labelInfo.setText("返回原程序");
}
}
else{
des=textArea.getText();
labelInfo.setText("退出程序");
if(sou.equals(des)==false){
int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");
if(len==JOptionPane.NO_OPTION) System.exit(0);
else{
if(len==JOptionPane.YES_OPTION){
len=len=openDialog.showSaveDialog(mainWindow);
File exitFile=openDialog.getSelectedFile();
des=textArea.getText();
save(exitFile);
if(exitFile.exists()) labelInfo.setText(exitFile.getPath()+"保存成功");
}
else labelInfo.setText("返回原程序");
}
}
else System.exit(0);
}
}
//“关于文本编辑器”菜单项处理动作
if(command.equals("关于文本编辑器")){
JOptionPane.showMessageDialog(null,"文本编辑器Version 0.7");
labelInfo.setText("返回原程序");
}
}
}
//打开文件方法
public void open(File file){
//String filePath=file.getParent()+"\\"+file.getName();
try{
FileReader in=new FileReader(file);
int len=in.read(buffer);
textArea.setText(new String(buffer,0,len));
sou=new String(buffer,0,len);
labelInfo.setText("打开"+file.getPath());
in.close();
}
catch(Exception e){}
}
//保存文件方法
public void save(File file){
try{
FileWriter out=new FileWriter(file);
buffer=des.toCharArray();
out.write(buffer);
out.close();
desTime=file.lastModified();
if(desTime!=souTime&&file.exists()) {
labelInfo.setText(file.getPath()+"保存成功");
souTime=desTime;}
}
catch(Exception e){}
}
public static void main(String[] args) {
new TextEditor().init();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
theme2002
2007-01-25 · 超过10用户采纳过TA的回答
知道答主
回答量:32
采纳率:0%
帮助的人:29.4万
展开全部
有200多行的代码

package 记事本;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.borland.jbcl.layout.PaneLayout;
import com.borland.jbcl.layout.*;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class NotepadFrame
extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JMenuItem jMenuFileOpen = new JMenuItem();
JMenuItem jMenuFileSave = new JMenuItem();
JMenuItem jMenuFileNew = new JMenuItem();
JMenu jMenuEdit = new JMenu();
JMenuItem jMenuFileSaveAs = new JMenuItem();
JTextPane jTextPane = new JTextPane();
PaneLayout paneLayout1 = new PaneLayout();
JMenu jMenuFormat = new JMenu();
JMenuItem jMenuFormatFont = new JMenuItem();
JMenuItem jMenuEditUodo = new JMenuItem();
JMenuItem jMenuEditCut = new JMenuItem();
JMenuItem jMenuEditCopy = new JMenuItem();
JMenuItem jMenuEditPaste = new JMenuItem();
JMenuItem jMenuEditDelete = new JMenuItem();
JMenuItem jMenuEditFind = new JMenuItem();
JMenuItem jMenuEditFindNext = new JMenuItem();
JMenuItem jMenuEditReplace = new JMenuItem();
JMenuItem jMenuEditGoto = new JMenuItem();
JMenuItem jMenuEditSelectAll = new JMenuItem();
JMenuItem jMenuEditTimeDate = new JMenuItem();
public NotepadFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(paneLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuFileExit.addActionListener(new
NotepadFrame_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new
NotepadFrame_jMenuHelpAbout_ActionAdapter(this));
jMenuFileOpen.setText("打开");
jMenuFileOpen.addActionListener(new
NotepadFrame_jMenuFileOpen_actionAdapter(this));
jMenuFileSave.setText("保存");
jMenuFileSave.addActionListener(new
NotepadFrame_jMenuFileSave_actionAdapter(this));
jMenuFileNew.setText("新建");
jMenuFileNew.addActionListener(new NotepadFrame_jMenuFileNew_actionAdapter(this));
jMenuEdit.setText("编辑");
jMenuFileSaveAs.setText("另存为");
jMenuFormat.setText("格式");
jMenuFormatFont.setText("字体");
jMenuEditUodo.setEnabled(false);
jMenuEditUodo.setText("撤销");
jMenuEditCut.setEnabled(false);
jMenuEditCut.setText("剪切");
jMenuEditCopy.setEnabled(false);
jMenuEditCopy.setText("复制");
jMenuEditPaste.setText("粘贴");
jMenuEditDelete.setEnabled(false);
jMenuEditDelete.setText("删除");
jMenuEditFind.setEnabled(false);
jMenuEditFind.setText("查找");
jMenuEditFindNext.setEnabled(false);
jMenuEditFindNext.setText("查找下一个");
jMenuEditReplace.setText("替换");
jMenuEditGoto.setEnabled(false);
jMenuEditGoto.setText("转到");
jMenuEditSelectAll.setText("全选");
jMenuEditTimeDate.setText("时间/日期");
jMenuBar1.add(jMenuFile);
jMenuFile.add(jMenuFileNew);
jMenuFile.add(jMenuFileOpen);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileSave);
jMenuFile.add(jMenuFileSaveAs);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuBar1.add(jMenuEdit);
jMenuBar1.add(jMenuFormat);
jMenuBar1.add(jMenuHelp);
jMenuHelp.add(jMenuHelpAbout);
contentPane.add(jTextPane,
new PaneConstraints("jTextPane1", "jTextPane1",
PaneConstraints.ROOT, 1.0F));
jMenuEdit.add(jMenuEditUodo);
jMenuEdit.addSeparator();
jMenuEdit.add(jMenuEditCut);
jMenuEdit.add(jMenuEditCopy);
jMenuEdit.add(jMenuEditPaste);
jMenuEdit.add(jMenuEditDelete);
jMenuEdit.addSeparator();
jMenuEdit.add(jMenuEditFind);
jMenuEdit.add(jMenuEditFindNext);
jMenuEdit.add(jMenuEditReplace);
jMenuEdit.add(jMenuEditGoto);
jMenuEdit.addSeparator();
jMenuEdit.add(jMenuEditSelectAll);
jMenuEdit.add(jMenuEditTimeDate);
jMenuFormat.add(jMenuFormatFont);
setJMenuBar(jMenuBar1);
}

/**
* File | Exit action performed.
*
* @param actionEvent ActionEvent
*/
void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}

/**
* Help | About action performed.
*
* @param actionEvent ActionEvent
*/
void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
NotepadFrame_AboutBox dlg = new NotepadFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
}

public void jMenuFileNew_actionPerformed(ActionEvent e) {
String txtNew=jTextPane.getText();
if(!txtNew.equals("")){
int num=JOptionPane.showConfirmDialog(this,"是否保存","保存",JOptionPane.YES_NO_CANCEL_OPTION);
if(num==0){
JFileChooser saver=new JFileChooser("C:\\Documents and Settings\\163\\My Documents\\");
saver.showSaveDialog(jTextPane);
}
else if(num==1){
jTextPane.setText("");
}
}
}

public void jMenuFileOpen_actionPerformed(ActionEvent e) {
jTextPane.setText("");
JFileChooser opener = new JFileChooser();
opener.showOpenDialog(this);
try{
FileReader filechoosed=new FileReader(opener.getSelectedFile());
BufferedReader rf=new BufferedReader(filechoosed);
String rl=rf.readLine();
while(!rl.equals("")){
jTextPane.setText(jTextPane.getText()+rl+"\n");
rl=rf.readLine();
}
}catch(Exception ex){
System.out.println("error");
}
}

public void jMenuFileSave_actionPerformed(ActionEvent e) {

}
}

class NotepadFrame_jMenuFileNew_actionAdapter
implements ActionListener {
private NotepadFrame adaptee;
NotepadFrame_jMenuFileNew_actionAdapter(NotepadFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {

adaptee.jMenuFileNew_actionPerformed(e);
}
}

class NotepadFrame_jMenuFileOpen_actionAdapter
implements ActionListener {
private NotepadFrame adaptee;
NotepadFrame_jMenuFileOpen_actionAdapter(NotepadFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jMenuFileOpen_actionPerformed(e);
}
}

class NotepadFrame_jMenuFileSave_actionAdapter
implements ActionListener {
private NotepadFrame adaptee;
NotepadFrame_jMenuFileSave_actionAdapter(NotepadFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jMenuFileSave_actionPerformed(e);
}
}

class NotepadFrame_jMenuFileExit_ActionAdapter
implements ActionListener {
NotepadFrame adaptee;

NotepadFrame_jMenuFileExit_ActionAdapter(NotepadFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuFileExit_actionPerformed(actionEvent);
}
}

class NotepadFrame_jMenuHelpAbout_ActionAdapter
implements ActionListener {
NotepadFrame adaptee;

NotepadFrame_jMenuHelpAbout_ActionAdapter(NotepadFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式