Chinaunix首页 | 论坛 | 博客
  • 博客访问: 434276
  • 博文数量: 54
  • 博客积分: 610
  • 博客等级: 上士
  • 技术积分: 721
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-24 10:52
文章分类

全部博文(54)

文章存档

2013年(27)

2012年(11)

2011年(16)

分类: Java

2011-02-15 22:27:12


  1. //需要的变量
  2.     private String myTitle ;
  3.     private String oldStr;//保存记事本中旧的数据,判断文件是否改变
  4.     //文件,文件流相关的变量
  5.     private JFileChooser fileChooser = new JFileChooser();
  6.     private FileFilter filter = new FileNameExtensionFilter("记事本文件(txt)","TXT");
  7.     private FileFilter sFilter = new FileNameExtensionFilter("记事本文件(txt)","TXT","*");
  8.     private File selectedFile ;
  9.     private FileReader fr;
  10.     private FileWriter fw;
  11.     private BufferedReader bufr;


1.打开文件
  1. private void openFile(){
  2.         
  3.         myJta.setText("");//清空此前数据
  4.         fileChooser.setFileFilter(filter);
  5.         
  6.         int i = fileChooser.showOpenDialog(getContentPane());
  7.         if(i==JFileChooser.APPROVE_OPTION){
  8.             selectedFile = fileChooser.getSelectedFile();
  9.             myTitle = selectedFile.getName();
  10.             setTitle(myTitle+"-记事本");
  11.             try {
  12.                 fr = new FileReader(selectedFile);
  13.                 bufr = new BufferedReader(fr);
  14.                 String tempS ="";
  15.                 while((tempS=bufr.readLine())!=null){
  16.                     myJta.append(tempS+"\r\n");    
  17.                 }
  18.                 bufr.close();
  19.                 fr.close();
  20.             } catch (Exception e1) {
  21.                 // TODO Auto-generated catch block
  22.                 e1.printStackTrace();
  23.             }
  24.             oldStr = myJta.getText();
  25.         }
  26.     }
2.新建文件
  1. private void newFile(){
  2.         int i = checkEdited();
  3.         if(i == JOptionPane.OK_OPTION){
  4.             if("无标题 -记事本".equals(myTitle)){
  5.                 saveOFile();
  6.             }
  7.             else{
  8.                 saveFile();
  9.             }
  10.             myTitle = "无标题 -记事本";
  11.             setTitle(myTitle);
  12.             myJta.setText("");//清空此前数据
  13.         }else if(i == JOptionPane.NO_OPTION){
  14.             myTitle = "无标题 -记事本";
  15.             setTitle(myTitle);
  16.             myJta.setText("");//清空此前数据
  17.         }else if(i==JOptionPane.CANCEL_OPTION){
  18.             return;
  19.         }
  20.     }
3.保存文件
  1. // save file function    
  2.     private void saveFile(){
  3. //        try{
  4. //            fw = new FileWriter(selectedFile);
  5. //            buwr = new BufferedWriter(fw);
  6. //            PrintWriter prwr = new PrintWriter(buwr);
  7. //            prwr.write(myJta.getText());
  8. //            prwr.close();
  9. //            buwr.close();
  10. //            fw.close();
  11. //        }catch(Exception e1){
  12. //            e1.printStackTrace();                
  13. //        }
  14. //用文件输出流,在换行时,发生错误
  15.         Document myDoc = myJta.getDocument();
  16.         DefaultEditorKit kit = new DefaultEditorKit();
  17.         try {
  18.             fw = new FileWriter(selectedFile);
  19.             kit.write(fw,myDoc,0,myDoc.getLength());
  20.         } catch (Exception e) {
  21.             // TODO Auto-generated catch block
  22.             e.printStackTrace();
  23.         }
  24.         
  25.     }
其中,用文件输出流时,总是换行时,如果用txt打开,会发现有乱码,换行处。
4.另存为
  1. //    save as function
  2.     private void saveOFile(){
  3.         fileChooser.setFileFilter(sFilter);
  4.         int i = fileChooser.showSaveDialog(getContentPane());
  5.         if(i==JFileChooser.APPROVE_OPTION){
  6.             selectedFile = fileChooser.getSelectedFile();
  7.             myTitle = selectedFile.getName();

  8.             if (selectedFile.exists())
  9.             {
  10.                 int overwriteSelect = JOptionPane.showConfirmDialog(this,
  11.                         "文件" + myTitle + "已存在,是否覆盖?",
  12.                         "是否覆盖?",
  13.                         JOptionPane.YES_NO_OPTION,
  14.                         JOptionPane.WARNING_MESSAGE);
  15.                 if (overwriteSelect != JOptionPane.YES_OPTION)
  16.                 {
  17.                     return;
  18.                 }
  19.             }
  20. // 如果文件不是以".txt"结尾,则提醒补充.txt
  21.             if (!myTitle.endsWith("txt"))
  22.             {
  23.                 JOptionPane.showMessageDialog(this, "请以.txt结尾", "后缀", JOptionPane.DEFAULT_OPTION);
  24.                 myTitle = "无标题 -记事本";
  25.                 return;
  26.             }
  27.             setTitle(myTitle+"-记事本");
  28.             saveFile();
  29.         }
  30.     }


阅读(10376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~