Java文件(io)编程——简易记事本开发

  1 public class NotePad extends JFrame implements ActionListener{
  2 
  3     //定义需要的组件
  4     JTextArea jta=null;                    //多行文本框
  5     
  6     JMenuBar jmb=null;                     //菜单条
  7     JMenu jm1=null;                        //菜单
  8     JMenuItem jmi1=null,jmi2=null;         //菜单项
  9     
 10     public static void main(String[] args) {
 11         NotePad np=new NotePad();
 12 
 13     }
 14     
 15     public NotePad(){                     //构造函数
 16         
 17         jta=new JTextArea();              //创建jta
 18         jmb=new JMenuBar();
 19         jm1=new JMenu("文件");
 20         jm1.setMnemonic('F');             //设置助记符
 21         
 22         jmi1=new JMenuItem("打开",new ImageIcon("imag_3.jpg"));    
 23         jmi1.addActionListener(this);     //注册监听
 24         jmi1.setActionCommand("open");
 25 
 26         jmi2=new JMenuItem("保存");
 27         jmi2.addActionListener(this);
 28         jmi2.setActionCommand("save");
 29         
 30         this.setJMenuBar(jmb);            //加入
 31         
 32         jmb.add(jm1);                     //把菜单放入菜单条
 33         
 34         jm1.add(jmi1);                    //把item放入到Menu中
 35         jm1.add(jmi2);
 36         
 37         this.add(jta);                   //放入到JFrame
 38         
 39         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 40         this.setSize(400,300);
 41         this.setTitle("记事本");
 42         this.setIconImage((new ImageIcon("imag_2.jpg")).getImage());
 43         this.setVisible(true);
 44     }
 45 
 46     @Override
 47     public void actionPerformed(ActionEvent arg0) {
 48         //判断是哪个菜单被选中
 49         if(arg0.getActionCommand().equals("open")){
 50             
 51             //JFileChooser,创建一个文件选择组件
 52             JFileChooser jfc1=new JFileChooser();
 53             jfc1.setDialogTitle("请选择文件……");  //设置名字
 54             
 55             jfc1.showOpenDialog(null);           //默认方式
 56             jfc1.setVisible(true);               //显示
 57             
 58             //得到用户选择的文件全路径
 59             String filename=jfc1.getSelectedFile().getAbsolutePath();
 60             
 61             FileReader fr=null;
 62             BufferedReader br=null;
 63             
 64             try {
 65                 fr=new FileReader(filename);
 66                 br=new BufferedReader(fr);
 67             
 68                 //从文件中读取信息并显示到jta
 69                 String s="";
 70                 String allCon="";
 71                 while((s=br.readLine())!=null){   //循环读取文件,s不为空即还未读完毕     
 72                     allCon+=s+"
";
 73                 } 
 74 
 75                 jta.setText(allCon);              //放置到jta
 76             
 77             } catch (Exception e) {
 78                 e.printStackTrace();
 79             }finally{
 80                 
 81                 try {
 82                     fr.close();
 83                     br.close();
 84                 } catch (Exception e) {
 85                     e.printStackTrace();
 86                 }
 87             }    
 88         }else if(arg0.getActionCommand().equals("save")){
 89             //出现保存对话框
 90             JFileChooser jfc2=new JFileChooser();
 91             jfc2.setDialogTitle("另存为……");
 92             jfc2.showSaveDialog(null);                //按默认的方式显示
 93             jfc2.setVisible(true);
 94             
 95             //得到用户希望把文件保存到何处,文件全路径
 96             String filename2=jfc2.getSelectedFile().getAbsolutePath();
 97             
 98             //准备写入到指定文件
 99             FileWriter fw=null;
100             BufferedWriter bw=null;
101             
102             try {
103                 fw=new FileWriter(filename2);
104                 bw=new BufferedWriter(fw);
105                 
106                 bw.write(this.jta.getText());
107             } catch (Exception e) {
108                 e.printStackTrace();
109             }finally{
110                 try {
111                     bw.close();
112                 } catch (IOException e) {
113                     e.printStackTrace();
114                 }
115             }
116         }
117     }
118 }

运行效果如下:

点击文件按钮,点击打开菜单项,选择一个文本文件,效果如下:

打开后,内容显示如下:

对内容稍作修改,另存为名为sss的文件,效果如下:

原文地址:https://www.cnblogs.com/cxq1126/p/7347894.html