分享

通用各类文档读写的设计与实现

 WindySky 2011-07-28

在我们日常的项目开发中,经常碰到需要读取word等文档的需求,如何来设计和实现呢?我的实现代码如下:

  1. 定义一个IDoc的接口

 

Java代码 复制代码
  1. package com.common.doc;   
  2.   
  3. public interface IDoc {   
  4.     public String read();   
  5.     public boolean write(String str);   
  6.     public boolean rename(String newName);   
  7.     public boolean exists();   
  8. }  
Java代码 复制代码 收藏代码
  1. package com.common.doc;   
  2.   
  3. public interface IDoc {   
  4.     public String read();   
  5.     public boolean write(String str);   
  6.     public boolean rename(String newName);   
  7.     public boolean exists();   
  8. }  
 
  1. 定义一个AbstractDoc的抽象类,这个类实现一般的文件操作,如存在判断等

 

Java代码 复制代码
  1. package com.common.doc;   
  2.   
  3. import java.io.File;   
  4.   
  5. public class AbstractDoc implements IDoc{   
  6.   
  7.     private String filepath;   
  8.        
  9.     public String getFilepath() {   
  10.         return filepath;   
  11.     }   
  12.     public AbstractDoc(String path){   
  13.         this.filepath = path;   
  14.     }   
  15.     public boolean exists() {   
  16.         File f=new File(filepath);   
  17.         return f.exists();   
  18.     }   
  19.     @Override  
  20.     public String read(){   
  21.         return "";   
  22.     }   
  23.   
  24.     public boolean rename(String newName) {   
  25.         try{   
  26.             File f=new File(filepath);   
  27.             String str=filepath.substring(0,filepath.lastIndexOf("\\"));   
  28.             f.renameTo(new File(str+"\\"+newName));   
  29.             }catch(Exception ex)   
  30.             {   
  31.               return false;   
  32.             }   
  33.             return true;   
  34.     }   
  35.     @Override  
  36.     public boolean write(String str) {   
  37.         return false;   
  38.     }   
  39.   
  40. }  
Java代码 复制代码 收藏代码
  1. package com.common.doc;   
  2.   
  3. import java.io.File;   
  4.   
  5. public class AbstractDoc implements IDoc{   
  6.   
  7.     private String filepath;   
  8.        
  9.     public String getFilepath() {   
  10.         return filepath;   
  11.     }   
  12.     public AbstractDoc(String path){   
  13.         this.filepath = path;   
  14.     }   
  15.     public boolean exists() {   
  16.         File f=new File(filepath);   
  17.         return f.exists();   
  18.     }   
  19.     @Override  
  20.     public String read(){   
  21.         return "";   
  22.     }   
  23.   
  24.     public boolean rename(String newName) {   
  25.         try{   
  26.             File f=new File(filepath);   
  27.             String str=filepath.substring(0,filepath.lastIndexOf("\\"));   
  28.             f.renameTo(new File(str+"\\"+newName));   
  29.             }catch(Exception ex)   
  30.             {   
  31.               return false;   
  32.             }   
  33.             return true;   
  34.     }   
  35.     @Override  
  36.     public boolean write(String str) {   
  37.         return false;   
  38.     }   
  39.   
  40. }  
 
  1. 各个文档继承AbstractDoc的read,write重载函数即可

如Wod实现代码如下:

Java代码 复制代码
  1. package com.common.doc;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.FileOutputStream;   
  5. import java.io.InputStream;   
  6. import java.io.OutputStream;   
  7.   
  8. import org.apache.poi.hwpf.extractor.WordExtractor;   
  9.   
  10. public class WordDoc extends AbstractDoc{   
  11.        
  12.     public WordDoc(String path){   
  13.         super(path);   
  14.     }   
  15.        
  16.     public String read(){   
  17.         StringBuffer sb = new StringBuffer();   
  18.         try {   
  19.             InputStream is = new FileInputStream(super.getFilepath());   
  20.             WordExtractor ex = new WordExtractor(is);// is是WORD文件的InputStream   
  21.             sb.append(ex.getText());   
  22.         } catch (Exception e) {   
  23.             e.printStackTrace();   
  24.         }   
  25.         return sb.toString();          
  26.     }   
  27. }  
Java代码 复制代码 收藏代码
  1. package com.common.doc;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.FileOutputStream;   
  5. import java.io.InputStream;   
  6. import java.io.OutputStream;   
  7.   
  8. import org.apache.poi.hwpf.extractor.WordExtractor;   
  9.   
  10. public class WordDoc extends AbstractDoc{   
  11.        
  12.     public WordDoc(String path){   
  13.         super(path);   
  14.     }   
  15.        
  16.     public String read(){   
  17.         StringBuffer sb = new StringBuffer();   
  18.         try {   
  19.             InputStream is = new FileInputStream(super.getFilepath());   
  20.             WordExtractor ex = new WordExtractor(is);// is是WORD文件的InputStream   
  21.             sb.append(ex.getText());   
  22.         } catch (Exception e) {   
  23.             e.printStackTrace();   
  24.         }   
  25.         return sb.toString();          
  26.     }   
  27. }  
 
  1. 测试代码
Java代码 复制代码
  1. IDoc doc = new WordDoc("c:\\测试文档.doc");   
  2. if(doc.exists()){   
  3.  System.out.println(doc.read());   
  4. }else{   
  5.  System.out.println("word不存在");   
  6. }  
Java代码 复制代码 收藏代码
  1. IDoc doc = new WordDoc("c:\\测试文档.doc");   
  2. if(doc.exists()){   
  3.  System.out.println(doc.read());   
  4. }else{   
  5.  System.out.println("word不存在");   
  6. }  

  欢迎各位拍砖讨论!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多