文件的打开和保存

  1 package com.soft.test;
  2 
  3 import java.io.*;
  4 import java.awt.*;
  5 import javax.swing.*;
  6 import javax.swing.filechooser.FileFilter;
  7 import javax.swing.plaf.FileChooserUI;
  8 import java.awt.event.*;
  9 /*
 10  * 记事本程序
 11  * */
 12 public class EDitplus extends JFrame implements ActionListener {
 13  String path = null;
 14  JTextArea jta = null;
 15  //菜单
 16  JMenuBar jmb = null;
 17  //菜单项
 18  JMenu jm1 = null;
 19  JMenuItem jmi1 = null;
 20  //构造函数
 21  public EDitplus() {
 22   //创建jta
 23   jta = new JTextArea();
 24   this.setTitle("Editplus");
 25   jta.setFont(new Font("微软雅黑",1,16));
 26   jta.setLineWrap(true);
 27   jta.setAutoscrolls(true);
 28   this.add(new JScrollPane(jta));
 29   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 30   this.setVisible(true);
 31   this.setSize(800,600);
 32   
 33   //创建菜单
 34   JMenuBar jmb = new JMenuBar();
 35   JMenu jm1 = new JMenu("文件(F)");
 36   //jm1.setMnemonic('F');
 37   JMenuItem jmi1 = new JMenuItem("打开(O)");
 38   JMenuItem jmi2 = new JMenuItem("保存(S)");
 39   this.setJMenuBar(jmb);
 40   jmb.add(jm1);
 41   setJMenuBar(jmb);
 42   jm1.add(jmi1);
 43   jm1.add(jmi2);
 44   jmb.add(jm1);
 45   //菜单监听
 46   jmi1.setActionCommand("Open");
 47   jmi1.addActionListener(this);
 48   
 49   jmi2.setActionCommand("Save");
 50   jmi2.addActionListener(this);
 51   
 52   //快捷键监听
 53   jta.addKeyListener(new KeyListener() { 
 54    @Override
 55    public void keyTyped(KeyEvent arg0) {}
 56    @Override
 57    public void keyReleased(KeyEvent e) {
 58     if(e.getKeyCode() == KeyEvent.VK_S && e.isControlDown())
 59     {
 60      //当按下Ctrl+S时调用保存
 61      if (path==null && getTitle().equals("Editplus"))
 62      {
 63       //调用有对话框的保存
 64       dialogSave();
 65      }else
 66      {
 67       //调用无对话框的保存
 68       noDialogSave();
 69      }
 70      setTitle(path);
 71     }
 72     if(e.getKeyCode()==KeyEvent.VK_O && e.isControlDown())
 73     {
 74      //当按下Ctrl+O时调用打开对话框
 75      dialogOpen();
 76     }
 77    }
 78    @Override
 79    public void keyPressed(KeyEvent arg0) {}
 80   });
 81  
 82  }
 83  
 84  public static void main(String[] args) {
 85   EDitplus ep = new EDitplus();
 86  }
 87  
 88  //有对话框的打开
 89  public void dialogOpen() {
 90   final JFileChooser jfc1 = new JFileChooser();
 91   jfc1.setDialogTitle("请选择要打开的文件");
 92   
 93   int falg = jfc1.showOpenDialog(null);
 94   jfc1.setVisible(true); 
 95   if (falg != JFileChooser.APPROVE_OPTION)
 96    return ; 
 97  
 98   if (!jfc1.getSelectedFile().getName().endsWith(".txt"))
 99   {
100    JOptionPane.showMessageDialog(null, "请打开文本文件!");
101    return;
102   }
103   path = jfc1.getSelectedFile().getAbsolutePath();
104   FileReader fr = null;
105   BufferedReader br = null;
106   String str = null;
107   try {
108    fr = new FileReader(new File(path));
109    br = new BufferedReader(fr);
110    while((str=br.readLine())!=null)
111    {
112     jta.append(str + "\r\n");
113    }
114    this.setTitle(path);
115   } catch (Exception e1) {
116    e1.printStackTrace();
117    JOptionPane.showMessageDialog(null, "打开失败,请确认打开文件是否为文本文件");
118   }
119   finally {
120    try {
121     if (fr!=null)
122      fr.close();
123     if (br!= null)
124      br.close();
125    } catch (IOException e1) {
126     e1.printStackTrace();
127    }
128   }
129  }
130  
131  //有对话框的保存
132  public void dialogSave() {
133   if (jta.getText().equals(""))
134    return;
135   JFileChooser jfc = new JFileChooser();
136   jfc.setDialogTitle("另存为……");
137   
138   int flag = jfc.showSaveDialog(null);
139   jfc.setVisible(true);
140   if (flag != JFileChooser.APPROVE_OPTION)
141    return ;
142   path = jfc.getSelectedFile().getAbsolutePath();
143   FileWriter fw = null;
144   BufferedWriter bw = null;
145   try {
146    fw = new FileWriter(path);
147    bw = new BufferedWriter(fw);
148    bw.write(jta.getText());
149    bw.flush();
150   } catch (IOException e1) {
151    JOptionPane.showMessageDialog(null, "保存失败,请确认磁盘是否可写");
152    e1.printStackTrace();
153   }finally {
154     
155    try {
156    if (fw!=null) 
157     fw.close();
158    if (bw!=null)
159    bw.close(); 
160    } catch (IOException e1) {
161     e1.printStackTrace();
162    }
163    this.setTitle(path);
164   }
165  }
166  
167  //无对话框的保存
168  public void noDialogSave() {
169   FileWriter fw = null;
170   BufferedWriter bw = null;
171   try {
172    fw = new FileWriter(path);
173    bw = new BufferedWriter(fw);
174    bw.write(jta.getText());
175    bw.flush();
176   } catch (IOException e1) {
177    JOptionPane.showMessageDialog(null, "保存失败,请确认磁盘是否可写");
178    e1.printStackTrace();
179   }finally {
180     
181    try {
182    if (fw!=null) 
183     fw.close();
184    if (bw!=null)
185    bw.close(); 
186    } catch (IOException e1) {
187     e1.printStackTrace();
188    }
189   }
190  }
191  
192  //菜单监听事件
193  public void actionPerformed(ActionEvent e) {
194   if (e.getActionCommand().equals("Open")) 
195   {
196    dialogOpen();
197   }else if (e.getActionCommand().equals("Save")) {
198    if (path==null && this.getTitle().equals("Editplus"))
199    {
200     //调用有对话框的保存
201     dialogSave();
202    }else
203    {
204     //调用无对话框的保存
205     noDialogSave();
206    }
207    this.setTitle(path);
208   }
209  }
210 }

用filereader和filewriter来进行读写

posted @ 2015-06-05 09:29  小军的代码库  阅读(381)  评论(0编辑  收藏  举报