package com.tongdainfo.common.parser;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
* 功能描述:文本解析类
* @author zhao
*
*/
public class ParserUtil {
private static String charset = "GBK";
private static Map<String,IParser> PARSER_MAP=new HashMap();
private static String CONFIG="com/tongdainfo/common/parser/parser.properties";
static {
initParaserMap();
}
public static void initParaserMap() {
Properties props = new Properties();
InputStream in= null;
try{
in= Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG);
props.load(in);}
catch(Exception e){
e.printStackTrace();
}
finally{
if(in!=null ){
try {in.close(); }
catch (Exception e) {}
}
}
Map<String,IParser> paraserMap=new HashMap();
paraserMap.put(null, new IParser(){
public String[] getFileTypes() {
return null;
}
public String readText(File file, String charset) throws Exception {
return "";
}});
if(props.isEmpty()) return ;
Iterator it= props.keySet().iterator();
while(it.hasNext()){
String key=(String) it.next();
String clazzStr=(String)props.get(key);
try {
Class clazzObj =Class.forName(clazzStr);
IParser parser= (IParser)clazzObj.newInstance();
paraserMap.put(key.toLowerCase(), parser);
} catch (Exception e) {
e.printStackTrace();
}
}
ParserUtil.PARSER_MAP=paraserMap;
}
private static String parserText (File file ,String extendsName,String encoding) throws Exception{
IParser parser =PARSER_MAP.get(extendsName);
if(parser==null) return "";
String text=parser.readText(file,encoding);
return text;
}
public static String readText(File file,String fileName,String charset)throws Exception{
String extName = getFileType(fileName);
return parserText (file ,extName,charset);
}
public static String readText(File file,String fileName)throws Exception {
String extName = getFileType(fileName);
return parserText (file ,extName,charset);
}
public static String readText(String filePath,String charset)throws Exception{
String extName = getFileType(filePath);
File file=new File(filePath);
return parserText (file ,extName,charset);
}
public static String readText(String filePath) throws Exception {
String extName = getFileType(filePath);
File file=new File(filePath);
return parserText (file ,extName,charset);
}
/**
* 功能描述:获取文件类型
* @param path
* @return
*/
private static String getFileType(String path) {
path = path.trim();
int i = path.lastIndexOf(".");
if (i != -1) {
String type = path.substring(i + 1, path.length()).toLowerCase();
return type;
} else {
return null;
}
}
/**
* 判断文件是否可以接受
* @param file
* @return
*/
public static boolean isAcceptability(File file) {
String type=getFileType(file.getName());
type=type.toLowerCase();
return PARSER_MAP.containsKey(type);
}
public static boolean isAcceptability(String filePath) {
String type=getFileType(filePath);
type=type.toLowerCase();
return PARSER_MAP.containsKey(type);
}
/**
private String standardize(String str) {
str = str.replaceAll("[\\r\\n]", "");
str = str.replaceAll("维普资讯", "");
str = str.replaceAll("http://www.cqvip.com", "");
return str;
}
* @throws Exception */
public static void main(String[] s) throws Exception{
File file=new File("D:/");
File[] files=file.listFiles();
File temp=null;
for(int i=0;i<files.length;i++){
temp=files[i];
if(temp.isDirectory()) continue;
System.out.println("file:"+temp.getAbsolutePath());
System.out.println("内容:"+ParserUtil.readText(temp.getAbsolutePath()));
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java读取doc,docx文件源码
共30个文件
class:9个
java:8个
properties:2个
5星 · 超过95%的资源 需积分: 47 34 下载量 70 浏览量
2013-06-26
10:13:59
上传
评论
收藏 21KB RAR 举报
温馨提示
java读取doc,docx文件源码,请大家参考。lib有在我的资源里边找
资源推荐
资源详情
资源评论
收起资源包目录
zfcg_doc.rar (30个子文件)
zfcg_doc
src
com
tongdainfo
common
parser
PdfParser.java 1KB
.#Word2007Parser.java.1.1 1KB
IParser.java 526B
Word98_2003Parser.java 1KB
parser.properties 188B
Word2007Parser.java 1KB
ParserUtil.java 4KB
TxtParser.java 1KB
test
CurrTime.java 888B
WordReader.java 1KB
.classpath 949B
.mymetadata 297B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
.myeclipse
.project 1KB
WebRoot
META-INF
MANIFEST.MF 36B
index.jsp 834B
WEB-INF
classes
com
tongdainfo
common
parser
Word98_2003Parser.class 2KB
Word2007Parser.class 1KB
.#Word2007Parser.java.1.1 1KB
ParserUtil$1.class 842B
PdfParser.class 2KB
parser.properties 188B
ParserUtil.class 4KB
TxtParser.class 2KB
IParser.class 293B
test
CurrTime.class 1KB
WordReader.class 1KB
lib
web.xml 371B
共 30 条
- 1
资源评论
- 我在风中大声地唱2013-09-05这个还好,有点用
- s10527210622014-06-18这个还好,有点用
- daijian27522014-01-07很不错,很大帮助
祈祷
- 粉丝: 26
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功