用Java编写简单记事本

功能/架构

一个简单的记事本软件需要具备如下两个功能:打开.txt文件和保存到.txt文件。
这两个功能在按钮监视器中实现。
因此该程序主要分为如下几个部分:

  • main方法
  • 构造函数(设计窗口)
  • 选择文件窗口(使用JFileChooser)
  • actionPerformed(ActionEvent e) (监视器)

效果:
在这里插入图片描述

选择文件窗口

把选中的文件存在File类的对象file中

void fileChooser(){
	JFileChooser jfc=new JFileChooser();
	//只能选择文件
	jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
	//筛选.txt文件
	jfc.addChoosableFileFilter(new FileNameExtensionFilter("文本文档",".txt"));
	//显示窗口
	jfc.showOpenDialog(UI.this);
	file=jfc.getSelectedFile();
}

打开功能

打开即读入文件,用BufferedReader实现。
因为中文编码有时将“\r\n”视为一个回车,而另一些编码将其视为两个回车,所以要逐行读入,避免出现乱码(空行)

	fileChooser();
	//防止出现NullPointerException错误
	if(file==null) return ;
	BufferedReader reader= null;
	try {
	    String temp,ans = "";
	    reader =new BufferedReader(new FileReader(file));
	    //逐行读入,防止出现空行
	    //因为中文编码有时将“\r\n”视为一个回车,而另一些编码将其视为两个回车
	    while((temp=reader.readLine())!=null){
	        ans=ans+temp+"\n";
	    }
	    jta.setText(ans);
	} catch (FileNotFoundException ex) {
	    JOptionPane.showMessageDialog(null, "未找到该文件!");
	} catch (IOException ex) {
	    JOptionPane.showMessageDialog(null, "读取失败!");
	}finally {
	    try {
	        reader.close();
	    } catch (IOException ex) {
	        JOptionPane.showMessageDialog(null, "意料之外的错误!");
	    }
	}

保存功能

使用BufferedWriter

	BufferedWriter writer = null;
	//若是新建的文件,则另存为
	if(file==null)
	   fileChooser();
	//防止出现NullPointerException错误
	if(file==null) return ;
	try {
	   writer = new BufferedWriter(new FileWriter(file));
	   writer.write(this.jta.getText());
	} catch (IOException ex) {
	   JOptionPane.showMessageDialog(null, "保存失败!");
	}finally {
	   try {
	       writer.close();
	   } catch (IOException ex) {
	       JOptionPane.showMessageDialog(null, "意料之外的错误!");
	   }
	}

完整代码

//青大学子勿用
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class UI extends JFrame implements ActionListener {
    File file=null;
    JTextArea jta;
    JMenuItem mi1;
    JMenuItem mi2;
    public static void main(String[] args){
        UI ui=new UI();
    }
    UI(){
        super(" 记事本");

        setMenu();
        jta=new JTextArea();
        add(jta);
        this.setBounds(550, 200, 800, 600);//设置对话框的位置和大小
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
    }
    //设置菜单
    void setMenu(){
        mi1=new JMenuItem("打开文件");
        mi2=new JMenuItem("保存文件");
        //实例菜单栏
        JMenuBar mb=new JMenuBar();
        //实例一个菜单项
        JMenu jMenu=new JMenu("文件");
        //设置菜单栏
        this.setJMenuBar(mb);
        //添加菜单项
        mb.add(jMenu);
        //添加子目录
        jMenu.add(mi1);
        jMenu.add(mi2);
        //添加监视器
        mi1.addActionListener(this);
        mi2.addActionListener(this);
    }
    //选择文件窗口
    void fileChooser(){
        JFileChooser jfc=new JFileChooser();
        //只能选择文件
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        //筛选.txt文件
        jfc.addChoosableFileFilter(new FileNameExtensionFilter("文本文档",".txt"));
        //显示窗口
        jfc.showOpenDialog(UI.this);
        file=jfc.getSelectedFile();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JMenuItem x= (JMenuItem) e.getSource();
        //打开
        if(x==mi1){
            fileChooser();
            //防止出现NullPointerException错误
            if(file==null) return ;
            BufferedReader reader= null;
            try {
                String temp,ans = "";
                reader =new BufferedReader(new FileReader(file));
                //逐行读入,防止出现空行
                //因为中文编码有时将“\r\n”视为一个回车,而另一些编码将其视为两个回车
                while((temp=reader.readLine())!=null){
                    ans=ans+temp+"\n";
                }
                jta.setText(ans);
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "未找到该文件!");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "读取失败!");
            }finally {
                try {
                    reader.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "意料之外的错误!");
                }
            }
        }
        //保存
        else{
            BufferedWriter writer = null;
            //若是新建的文件,则另存为
            if(file==null)
                fileChooser();
            //防止出现NullPointerException错误
            if(file==null) return ;
            try {
                writer = new BufferedWriter(new FileWriter(file));
                writer.write(this.jta.getText());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "保存失败!");
            }finally {
                try {
                    writer.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "意料之外的错误!");
                }
            }
        }
    }
}

参考/借鉴

Java学习笔记之JFileChooser的简单使用https://blog.csdn.net/weixin_42294984/article/details/82707409
JAVA_IO_FileReader读取txt文件乱码问题https://blog.csdn.net/qq_40646143/article/details/84142746
java实现简单记事本https://blog.csdn.net/LIAO_7053/article/details/82195073

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iusehandle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值