赞
踩
毫无疑问支持.doc和.docx的只有POI
好多jar都是积分下载,要么就是官网下载,真心垃圾,jar包免费下载:
链接:https://pan.baidu.com/s/14MWWHN3cTsr0mmxMGkIObQ
提取码:93ob
复制这段内容后打开百度网盘手机App,操作更方便哦
新增支持读取txt文本和pdf,pdf需要iTextpdf.jar包,从上方链接下载即可
/** * 读取txt文件 * * @param filepath 本地txt或log文件 * @return 返回读取到的文件内容 */ public static String readFileContent(String filepath) { String content = ""; try { InputStream is = new FileInputStream(new File(filepath)); InputStreamReader reader = new InputStreamReader(is); BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) { content = content + line + "\n"; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.toString(); } return content; } /** * 读取word内容 * * @param path */ public static String readDoc(String path) { String content = ""; try { FileInputStream in = new FileInputStream(path); //PoiFs 主要类 管理整个文件系统生命周期 POIFSFileSystem pfs = new POIFSFileSystem(in); //获取文档所有的数据结构 可以说是一个“文档对象” HWPFDocument hwpf = new HWPFDocument(pfs); content = hwpf.getText().toString(); } catch (Exception e) { e.printStackTrace(); } return content; } /** * 读取word内容 * * @param path */ public static String readDocx(String path) { String content = ""; try { InputStream is = new FileInputStream(path); XWPFDocument doc = new XWPFDocument(is); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); content = extractor.getText(); } catch (Exception e) { e.printStackTrace(); } return content; } /** * 读取Pdf文件的内容 * * @param path :文件地址 */ public String readPdf(String path) { String content = ""; try { PdfReader pr = new PdfReader(path); int page = pr.getNumberOfPages(); for (int i = 1; i < page + 1; i++) { content += PdfTextExtractor.getTextFromPage(pr, i); } } catch (IOException e) { e.printStackTrace(); } return content; }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。