关于java解析word的练习
package jim.java.ExtracorWord;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.textmining.text.extraction.WordExtractor;
public class ExtracorWord {
public static String getText(String doc){
String text = "";
try{
//创建读入流读取DOC文件
FileInputStream in = new FileInputStream(new File(doc));
//创建WordExtractor
WordExtractor extractor = new WordExtractor();
//提取文本
text = extractor.extractText(in);
}catch(Exception e){
text = " ";
}
return text;
}
public static void toTextFile(String doc,String txt) throws IOException{
//提取文本
String text = getText(doc);
//写入文本
PrintWriter pw = new PrintWriter(new FileWriter(new File(txt)));
pw.write(text);
pw.flush();
pw.close();
System.out.println(text);
System.out.println("成功写入文本文件"+txt);
}
public static void main(String[] args) throws IOException{
String text = getText("Hello.doc");
System.out.println(text);
toTextFile("Hello.doc","word.txt");
}
}
关于java解析html的练习:
package jim.Lius;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import lius.index.html.JTidyHtmlIndexer;
public class ExtractorAll {
public static String getText(String doc) {
String text = "";
String ext = doc.substring(doc.lastIndexOf(".")+1);
if(ext.equalsIgnoreCase("html")||ext.equalsIgnoreCase("html")||ext.equalsIgnoreCase("shtml")){
JTidyHtmlIndexer ji = new JTidyHtmlIndexer();
File f = new File(doc);
try {
ji.setStreamToIndex(new FileInputStream(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = ji.getContent();
try {
text = new String(text.getBytes("utf8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(text);
}
return text;
}
public static void toTextFile(String doc,String txt) throws IOException{
//提取文本
String text = getText(doc);
//写入文本
PrintWriter pw = new PrintWriter(new FileWriter(new File(txt)));
pw.write(text);
pw.flush();
pw.close();
System.out.println("成功写入文本文件"+txt);
}
public static void main(String[] args ){
String text = getText("Hello.html");
System.out.println(text);
try {
toTextFile("Hello.html","Hello.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
今天的收获:
了解了xml以及Dom4j基本入门了
今天的不足:
今天的效率有点低.