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、txt文件源码
共41个文件
jar:14个
class:9个
java:8个
4星 · 超过85%的资源 需积分: 19 154 下载量 70 浏览量
2011-11-06
11:38:14
上传
评论 2
收藏 10.51MB RAR 举报
温馨提示
实现解析doc和docx里面的内容,包括样式等,将读取出来的内容输出。
资源推荐
资源详情
资源评论
收起资源包目录
java 读取doc docx txt 文件例子.rar (41个子文件)
test
src
poi-ooxml-schemas-3.6-20091214.jar 3.6MB
poi-3.6-20091214.jar 1.47MB
com
tongdainfo
common
parser
PdfParser.java 1KB
.#Word2007Parser.java.1.1 1KB
IParser.java 526B
Word98_2003Parser.java 1KB
CVS
Entries 351B
Repository 45B
Root 31B
parser.properties 188B
Word2007Parser.java 1KB
ParserUtil.java 4KB
TxtParser.java 1KB
poi-ooxml-3.6-20091214.jar 403KB
xmlbeans-qname.jar 2KB
poi-scratchpad-3.6-20091214.jar 777KB
poi-contrib-3.6-20091214.jar 68KB
xmlParserAPIs.jar 121KB
test
CurrTime.java 888B
WordReader.java 1KB
bin
poi-ooxml-schemas-3.6-20091214.jar 3.6MB
poi-3.6-20091214.jar 1.47MB
com
tongdainfo
common
parser
Word98_2003Parser.class 2KB
Word2007Parser.class 3KB
.#Word2007Parser.java.1.1 1KB
ParserUtil$1.class 842B
PdfParser.class 2KB
parser.properties 188B
ParserUtil.class 5KB
TxtParser.class 3KB
IParser.class 293B
poi-ooxml-3.6-20091214.jar 403KB
xmlbeans-qname.jar 2KB
poi-scratchpad-3.6-20091214.jar 777KB
poi-contrib-3.6-20091214.jar 68KB
xmlParserAPIs.jar 121KB
test
CurrTime.class 1KB
WordReader.class 2KB
.classpath 942B
.settings
org.eclipse.core.resources.prefs 88B
.project 380B
共 41 条
- 1
hcs371239924
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页