Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23633
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 207
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-26 09:51
文章分类

全部博文(20)

分类: Java

2013-11-04 17:41:08


  1. import javax.swing.JFileChooser;
  2. import javax.swing.filechooser.FileFilter;

  3. class FileChoose{
  4.     /*
  5.      * the class for file save and file choose
  6.      */
  7.     private JFileChooser fc=new JFileChooser();
  8.     public FileChoose(){
  9.         cfFileFilter filter = new cfFileFilter();
  10.         fc.addChoosableFileFilter(filter);
  11.         fc.setFileFilter(filter);
  12.     }
  13.     
  14.     public void save_conf(String confContent){
  15.         /*
  16.          * show save dialog
  17.          */
  18.         fc.setDialogTitle("Save lab conf file");
  19.         int rc = fc.showSaveDialog(fc);
  20.         if(rc==JFileChooser.APPROVE_OPTION){
  21.             File file = fc.getSelectedFile();
  22.             //System.out.println(file.getPath());

  23.             save_data(confContent,file.getPath());
  24.         }
  25.             
  26.     }
  27.     private void save_data(String confContent,String path){
  28.         /*
  29.          * save info to cf file
  30.          */
  31.         try{
  32.         FileWriter f = new FileWriter(path);
  33.         f.write(confContent);
  34.         f.close();
  35.         }catch (IOException e) {
  36.             e.printStackTrace();
  37.         }

  38.     }
  39.     
  40.     
  41.     public List<String> select_conf(){
  42.         /*
  43.          * show file choosen dialog
  44.          */
  45.         fc.setDialogTitle("Select lab conf file");
  46.         int rc = fc.showOpenDialog(fc);
  47.         if(rc==JFileChooser.APPROVE_OPTION){
  48.             File file = fc.getSelectedFile();
  49.             //System.out.println(file.getPath());

  50.             List<String> datas = get_data(file.getPath());
  51.             return datas;
  52.         }
  53.         return null;
  54.             
  55.     }
  56.     
  57.     private List<String> get_data(String path){
  58.         /*
  59.          * get lab info from cf file
  60.          */
  61.         List<String> datas = new ArrayList<String>();
  62.         try{
  63.             BufferedReader f = new BufferedReader(new FileReader(path));
  64.             String line = null;
  65.             while((line = f.readLine()) !=null){
  66.                 datas.add(line);
  67.             }
  68.             f.close();
  69.         }catch (IOException e) {
  70.             e.printStackTrace();
  71.         }
  72.         
  73.         return datas;
  74.     }
  75.     
  76. }

  77. class cfFileFilter extends FileFilter {
  78.     /*
  79.      * (non-Javadoc)
  80.      * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
  81.      * the class extends from FileFilter to filter files except .cf file and directory
  82.      * show .cf file and directory only on file choosen dialog
  83.      */
  84.     public boolean accept(File file) {
  85.         String name = file.getName();
  86.         if(file.isDirectory()){
  87.             return true;
  88.         }
  89.         
  90.         return name.toLowerCase().endsWith(".cf");
  91.     }

  92.     @Override
  93.     public String getDescription() {
  94.         // TODO Auto-generated method stub

  95.         return "*.cf";
  96.     }     
  97. }

The class extends FileFilter must overwrite two methods ,
one is public boolean accept(File file)
the other is public String getDescription()
阅读(157) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~