以下是Java对几种文本文件内容读取代码。其中,OFFICE文档(WORD,EXCEL)使用了POI控件,PDF使用了PDFBOX控件。WORD
Java代码package textReader;
import java.io.*;
import org.apache.poi.hwpf.extractor.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 = wordExtractor.getText();
}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;
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
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一个行
for (int cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值
switch(aCell.getCellType()){
case HSSFCell.CELL_TYPE_FORMULA:
break;
case HSSFCell.CELL_TYPE_NUMERIC:
buff.append(aCell.getNumericCellValue()).append('\t');break;
case HSSFCell.CELL_TYPE_STRING:
buff.append(aCell.getStringCellValue()).append('\t');break;
}
}
}
buff.append('\n');
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buff.toString();
}
}
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 文件路径
* @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 = parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
result = stripper.getText(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.*;
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");
}
br.close();
return buff.toString();
}
}
package textReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.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 String(styledDoc.getText(0,styledDoc.getLength()).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的全部内容
*/
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((temp=br.readLine())!=null){
sb.append(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* @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;
};
return buff.toString();
分享到:
相关推荐
JAVA读取WORD_EXCEL_POWERPOINT_PDF文件的方法(poi)
JAVA读取PDF、WORD、EXCEL等文件的方法
整理了用java如何读取word文档,pdf文档的几种方法,含有程序
JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
收集的java操作文件的例子,很全面,需要的朋友可以看下
java读取word,excel,pdf等文本
包括中文字符处理,直接是个工程包,可以直接用, 主要用到的包 *需要引入的包(附件中都在) ... * itextpdf-5.4.2.jar * pdfbox-app-1.8.2.jar * antlr-2.7.6.jar * chardet-1.0.jar * cpdetector_1.0.8.jar
可以完整的读取word文件Excel文件PDF文件Txt文件,以文本的形式读出来,简单易懂
Java读取Word中的表格(Excel),并导出文件为Excel
该项目完美实现word、pdf、txt、excel的读取功能
Java读取Excel内容 v Java读取Excel内容 Java读取Excel内容
java 读取Excel文件中的内容 java 读取Excel文件中的内容
java 读取PDF文件中的内容 java 读取PDF文件中的内容
Java读取Word文档中指定位置(可以自己自定义位置)的表格数据或文本内容 * @param filePath 文档路径 * @param start 指定位置开始读取表格数据的该位置上的字符串 * @param end 指定位置开始结束读取表格数据的该...
使用Java读取Excel文件的内容,主要是使用POI框架,可以读取最新的Excel文件xlsx后缀文件,即2007版本之后的。
通过Java读取word表格中的内容,将内容存到数据库中,将Word中的图片存到硬盘中
JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
java读写Excel,POI.JAR,Word内容读取
读取Doc,Excel,PDF,html,生成Txt文件,读取Txt生成Excel文件 jar 所需用的jar文件: fontbox-0.1.0.jar PDFBox-0.7.3.jar poi-3.0.1.jar tm-extractors-0.4.jar