每日一练

关于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基本入门了

今天的不足:

今天的效率有点低.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值