JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
2012-06-29 17:13:08| 分类: JAVA | 标签: |字号大中小 订阅
以下是Java对几种文本文件内容读取代码。其中,OFFICE文档(WORD,EXCEL)使用了POI控件,PDF使用了PDFBOX控件。
查看相关控件的下载地址和配置方法。
转自:hi.baidu/allan811112/blog/item/d77b70492f0a65fb82025c3b.html
WORD
Java代码
package textReader;
import java.io.*;
import org.apache.actor.WordExtractor;
public class WordReader {
public WordReader(){
Java代码
package textReader;
import java.io.*;
import org.apache.actor.WordExtractor;
public class WordReader {
public WordReader(){
}
/**
* @param filePath 文件路径
* @return 读出的Word的内容
*/
public String getTextFromWord(String filePath){
String result = null;
File file = new File(filePath);
try{
FileInputStream fis = new FileInputStream(file);
WordExtractor wordExtractor = new WordExtractor(fis);
result = Text();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
/**
* @param filePath 文件路径
* @return 读出的Word的内容
*/
public String getTextFromWord(String filePath){
String result = null;
File file = new File(filePath);
try{
FileInputStream fis = new FileInputStream(file);
WordExtractor wordExtractor = new WordExtractor(fis);
result = Text();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
};
return result;
}
}
EXCEL
Java代码
package textReader;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
};
return result;
}
}
EXCEL
Java代码
package textReader;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExcelReader {
@SuppressWarnings("deprecation")
/**
* @param filePath 文件路径
* @return 读出的Excel的内容
*/
public String getTextFromExcel(String filePath) {
StringBuffer buff = new StringBuffer();
try {
//创建对Excel工作簿文件的引用
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
//创建对工作表的引用。?????????
for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) {
if (null != wb.getSheetAt(numSheets)) {
HSSFSheet aSheet = wb.getSheetAt(numSheets);//获得一个sheet
@SuppressWarnings("deprecation")
/**
* @param filePath 文件路径
* @return 读出的Excel的内容
*/
public String getTextFromExcel(String filePath) {
StringBuffer buff = new StringBuffer();
try {
//创建对Excel工作簿文件的引用
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
//创建对工作表的引用。?????????
for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) {
if (null != wb.getSheetAt(numSheets)) {
HSSFSheet aSheet = wb.getSheetAt(numSheets);//获得一个sheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= LastRowNum(); rowNumOfSheet++) {
if (null != Row(rowNumOfSheet)) {
HSSFRow aRow = Row(rowNumOfSheet); //获得一个行
for (int cellNumOfRow = 0; cellNumOfRow <= LastCellNum(); cellNumOfRow++) {
if (null != Cell(cellNumOfRow)) {
HSSFCell aCell = Cell(cellNumOfRow);//获得列值
CellType()){
case HSSFCell.CELL_TYPE_FORMULA:
break;??
case HSSFCell.CELL_TYPE_NUMERIC:
buff.NumericCellValue()).append('\t');break;
case HSSFCell.CELL_TYPE_STRING:
buff.StringCellValue()).append('\t');break;??????????????????????????????????????????????????????????????
if (null != Row(rowNumOfSheet)) {
HSSFRow aRow = Row(rowNumOfSheet); //获得一个行
for (int cellNumOfRow = 0; cellNumOfRow <= LastCellNum(); cellNumOfRow++) {
if (null != Cell(cellNumOfRow)) {
HSSFCell aCell = Cell(cellNumOfRow);//获得列值
CellType()){
case HSSFCell.CELL_TYPE_FORMULA:
break;??
case HSSFCell.CELL_TYPE_NUMERIC:
buff.NumericCellValue()).append('\t');break;
case HSSFCell.CELL_TYPE_STRING:
buff.StringCellValue()).append('\t');break;??????????????????????????????????????????????????????????????
}????????????????????????????
}?????????????????????????????????????????????????????????????????????????????????
}
buff.append('\n');
}
}???????????????????????????????????????
}????????????
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String();
}?????????????
}?????????????????????????????????????????????????????????????????????????????????
}
buff.append('\n');
}
}???????????????????????????????????????
}????????????
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String();
}?????????????
}
PDF
Java代码
package textReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public PdfReader(){
}
/**
* @param filePath 文件路径
Java代码
package textReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public PdfReader(){
}
/**
* @param filePath 文件路径
* @return 读出的pdf的内容
*/
public String getTextFromPdf(String filePath) {
String result = null;
FileInputStream is = null;
PDDocument document = null;
try {
is = new FileInputStream(filePath);
PDFParser parser = new PDFParser(is);
parser.parse();
document = PDDocument();
PDFTextStripper stripper = new PDFTextStripper();
result = Text(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
*/
public String getTextFromPdf(String filePath) {
String result = null;
FileInputStream is = null;
PDDocument document = null;
try {
is = new FileInputStream(filePath);
PDFParser parser = new PDFParser(is);
parser.parse();
document = PDDocument();
PDFTextStripper stripper = new PDFTextStripper();
result = Text(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {is.close();}catch(IOException e){e.printStackTrace();}
}
if (document != null) {
try{document.close();}catch (IOException e){e.printStackTrace();}
}
}
return result;
}
}
package textReader;
import java.io.*;
e.printStackTrace();
} finally {
if (is != null) {
try {is.close();}catch(IOException e){e.printStackTrace();}
}
if (document != null) {
try{document.close();}catch (IOException e){e.printStackTrace();}
}
}
return result;
}
}
package textReader;
import java.io.*;
public class TxtReader {
public TxtReader() {???????
}
/**
* @param filePath 文件路径
* @return 读出的txt的内容
*/
public String getTextFromTxt(String filePath) throws Exception {
FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);
StringBuffer buff = new StringBuffer();
String temp = null;
while((temp = br.readLine()) != null){
buff.append(temp + "\r\n");
}
public TxtReader() {???????
}
/**
* @param filePath 文件路径
* @return 读出的txt的内容
*/
public String getTextFromTxt(String filePath) throws Exception {
FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);
StringBuffer buff = new StringBuffer();
String temp = null;
while((temp = br.readLine()) != null){
buff.append(temp + "\r\n");
}
br.close();???????
String();???????
}
}
package textReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import BadLocationException;
import DefaultStyledDocument;
import tf.RTFEditorKit;
public class RtfReader {
public RtfReader(){
}
/
String();???????
}
}
package textReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import BadLocationException;
import DefaultStyledDocument;
import tf.RTFEditorKit;
public class RtfReader {
public RtfReader(){
}
/
**
* @param filePath 文件路径
* @return 读出的rtf的内容
*/
public String getTextFromRtf(String filePath) {
String result = null;
File file = new File(filePath);
try {???????
DefaultStyledDocument styledDoc = new DefaultStyledDocument();
InputStream is = new FileInputStream(file);
new RTFEditorKit().read(is, styledDoc, 0);
result = new Text(Length()).getBytes("ISO8859_1"));
//提取文本,读取中文需要使用ISO8859_1编码,否则会出现乱码
} catch (IOException e) {
e.printStackTrace();
* @param filePath 文件路径
* @return 读出的rtf的内容
*/
public String getTextFromRtf(String filePath) {
String result = null;
File file = new File(filePath);
try {???????
DefaultStyledDocument styledDoc = new DefaultStyledDocument();
InputStream is = new FileInputStream(file);
new RTFEditorKit().read(is, styledDoc, 0);
result = new Text(Length()).getBytes("ISO8859_1"));
//提取文本,读取中文需要使用ISO8859_1编码,否则会出现乱码
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
return result;
}
}
package textReader;
import java.io.*;
public class HtmlReader {
public HtmlReader() {
}
/**
* @param filePath 文件路径
* @return 获得html的全部内容
*/
e.printStackTrace();
}
return result;
}
}
package textReader;
import java.io.*;
public class HtmlReader {
public HtmlReader() {
}
/**
* @param filePath 文件路径
* @return 获得html的全部内容
*/
public String readHtml(String filePath) {
BufferedReader br=null;
StringBuffer sb = new StringBuffer();
try {
br=new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "GB2312"));???????????
String temp=null;???????
while((adLine())!=null){
sb.append(temp);
}???????????
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
java修改html文件BufferedReader br=null;
StringBuffer sb = new StringBuffer();
try {
br=new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "GB2312"));???????????
String temp=null;???????
while((adLine())!=null){
sb.append(temp);
}???????????
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String();
}
/**
* @param filePath 文件路径
* @return 获得的html文本内容
*/
public String getTextFromHtml(String filePath) {
//得到body标签中的内容
String str= readHtml(filePath);
StringBuffer buff = new StringBuffer();
int maxindex = str.length() - 1;
int begin = 0;
int end;???????????
//截取>和<之间的内容
while((begin = str.indexOf('>',begin)) < maxindex){???????????
}
/**
* @param filePath 文件路径
* @return 获得的html文本内容
*/
public String getTextFromHtml(String filePath) {
//得到body标签中的内容
String str= readHtml(filePath);
StringBuffer buff = new StringBuffer();
int maxindex = str.length() - 1;
int begin = 0;
int end;???????????
//截取>和<之间的内容
while((begin = str.indexOf('>',begin)) < maxindex){???????????
end = str.indexOf('<',begin);
if(end - begin > 1){
buff.append(str.substring(++begin, end));???????????????
}???????????
begin = end+1;
};???????
String();
}
}
if(end - begin > 1){
buff.append(str.substring(++begin, end));???????????????
}???????????
begin = end+1;
};???????
String();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论