- 浏览: 36898 次
- 性别:
- 来自: 郑州
文章分类
最新评论
-
lujingjs:
什么嘛,这个就不能用
java中如何读取properties文件 -
mercyblitz:
AffixFile? 什么意思?
如何以流保存大字段Body
一个可读取:Excel,html,pdf,txt,word工具类:
package com.topsoft.info.services; /** * Created by IntelliJ IDEA. * User: * Date: 2009-10-9 * Time: 10:40:12 * To change this template use File | Settings | File Templates. */ import java.io.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.htmlparser.Node; import org.htmlparser.NodeFilter; import org.htmlparser.Parser; import org.htmlparser.filters.NodeClassFilter; import org.htmlparser.filters.OrFilter; import org.htmlparser.nodes.TextNode; import org.htmlparser.tags.Div; import org.htmlparser.tags.FormTag; import org.htmlparser.tags.LinkTag; import org.htmlparser.tags.MetaTag; import org.htmlparser.tags.StyleTag; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; import org.pdfbox.pdfparser.PDFParser; import org.pdfbox.util.PDFTextStripper; public class FileParseDomainImpl implements FileParseDomain { private static final Log log = LogFactory .getLog(FileParseDomainImpl.class); /** * 读取Excel类型的文件 * * @param filePath 文件路径 * @return 读取后返回的饿字符串 */ public String readExcel(String filePath) { String content = "";// 字符串 File file = new File(filePath); if (!file.exists()) { return ""; } InputStream in = null; try { in = new FileInputStream(filePath); HSSFWorkbook workbook = new HSSFWorkbook(in); ExcelExtractor extractor = new ExcelExtractor(workbook); extractor.setFormulasNotResults(false); extractor.setIncludeSheetNames(false); content = extractor.getText(); } catch (Exception ex) { log.debug("读取excel文件出错" + ex.getMessage(), ex); throw new RuntimeException("读取excel文件出错" + ex.getMessage()); // ex.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return content.trim(); } /** * 读取html类型的文件 * * @param filePath 文件路径 * @return 读取后返回的字符串 */ public String readHtml(String filePath) { StringBuffer content = new StringBuffer(""); String line = null;// 行 try { File file = new File(filePath); if (!file.exists()) { return ""; } FileInputStream fis = null; fis = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader( fis, "gbk"));// 这里的字符编码要注意,要对上html头文件的一致,否则会出乱码 while ((line = reader.readLine()) != null) { content.append(line + "\n"); } reader.close(); } catch (Exception ex) { log.debug("读取html或htm文件出错" + ex.getMessage(), ex); throw new RuntimeException("读取html或htm文件出错" + ex.getMessage()); // ex.printStackTrace(); } return content.toString(); } /** * 读取pdf类型的文件 * * @param filePath 文件路径 * @return 读取后返回的字符串 */ public String readPDF(String filePath) { File file = new File(filePath); if (!file.exists()) { return ""; } StringBuffer content = new StringBuffer("");// 文档内容 FileInputStream fis = null; try { fis = new FileInputStream(filePath); PDFParser p = new PDFParser(fis); p.parse(); PDFTextStripper ts = new PDFTextStripper(); content.append(ts.getText(p.getPDDocument())); } catch (Exception ex) { log.debug("读取pdf文件出错" + ex.getMessage(), ex); throw new RuntimeException("读取pdf文件出错" + ex.getMessage()); // ex.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } return content.toString(); } /** * 读取文本类型的文件 * * @param filePath 文件路径 * @return 读取后的字符串 */ public String readTxt(String filePath) { File file = new File(filePath); if (!file.exists()) { return ""; } StringBuffer content = new StringBuffer("");// 文档内容 String s1 = null; try { FileReader reader = new FileReader(filePath); BufferedReader br = new BufferedReader(reader); while ((s1 = br.readLine()) != null) { content.append(s1 + "\r"); } br.close(); reader.close(); } catch (Exception e) { log.debug("读取文本文件出错" + e.getMessage(), e); throw new RuntimeException("读取文本文件出错" + e.getMessage()); // e.printStackTrace(); } return content.toString().trim(); } /** * 读取word类型的文件 * * @param filePath 文件路径 * @return 读取后的字符串 */ public String readWord(String filePath) { File file = new File(filePath); if (!file.exists()) { return ""; } StringBuffer text = new StringBuffer(""); FileInputStream in = null; try { in = new FileInputStream(filePath); HWPFDocument doc = new HWPFDocument(in); Range range = doc.getRange(); int paragraphCount = range.numParagraphs();// 段落 for (int i = 0; i < paragraphCount; i++) {// 遍历段落读取数据 Paragraph pp = range.getParagraph(i); text.append(pp.text()); } in.close(); } catch (Exception ex) { log.debug("读取word文件出错" + ex.getMessage(), ex); throw new RuntimeException("读取word文件出错" + ex.getMessage()); // ex.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } String str = text.toString(); str = str.replaceAll(".*?", ""); str = str.replaceAll("\\d*?", ""); str = str.replaceAll("[]", ""); // str = str.replaceAll("\\r{2}\\r+", "\\r"); // System.out.println(str); return str; } /** * 读取html类温江并过滤标签 */ public String readHtmlText(String filePath) { File file = new File(filePath); if (!file.exists()) { return ""; } String htmlText = readHtml(filePath); Parser myParser; NodeList nodeList = null; StringBuffer result = new StringBuffer(); myParser = Parser.createParser(htmlText, "GBK"); NodeFilter textFilter = new NodeClassFilter(TextNode.class); NodeFilter linkFilter = new NodeClassFilter(LinkTag.class); NodeFilter metaFilter = new NodeClassFilter(MetaTag.class); NodeFilter styleFilter = new NodeClassFilter(StyleTag.class); NodeFilter divFilter = new NodeClassFilter(Div.class); NodeFilter formFilter = new NodeClassFilter(FormTag.class); OrFilter lastFilter = new OrFilter(); lastFilter.setPredicates(new NodeFilter[]{textFilter, linkFilter, metaFilter, styleFilter, divFilter, formFilter}); try { nodeList = myParser.parse(lastFilter); } catch (ParserException e) { log.debug("html过滤出错(readHtmlText)" + e.getMessage(), e); throw new RuntimeException("html过滤出错(readHtmlText)" + e.getMessage()); // log.info("html过滤出错(readHtmlText)"); // e.printStackTrace(); //To change body of catch statement use File // | Settings | File Templates. } Node[] nodes = nodeList.toNodeArray(); String line = ""; for (int i = 0; i < nodes.length; i++) { Node anode = nodes[i]; if (anode instanceof TextNode) { TextNode textnode = (TextNode) anode; line = textnode.getText(); } else if (anode instanceof LinkTag) { LinkTag linknode = (LinkTag) anode; line = linknode.getLink(); } if (isTrimEmpty(line)) continue; result.append(line); } return result.toString().replaceAll(" ", ""); } // public String officeToHtml(String paths, String savepaths) { // File d = new File(paths); // String filename = d.getName(); // // 判断是否为doc文件 // File s = new File(savepaths + "\\temp"); // boolean fl = s.exists(); // if (!fl) { // s.mkdir(); // } // String tpFile = savepaths + "\\temp\\" + filename.substring(0, // (filename.length() - 4)) + ".html"; // boolean tpexist = new File(tpFile).exists(); // if ((paths.endsWith(".doc") || paths.endsWith(".xls")) && !tpexist) { // String type = ""; // String property = ""; // Variant variant = null; // if (paths.endsWith(".doc")) { // type = "Word.Application"; // property = "Documents"; // variant = new Variant(8); // } else if (paths.endsWith(".xls")) { // type = "Excel.Application"; // property = "Workbooks"; // variant = new Variant(44); // } // // 打印当前目录路径 // ActiveXComponent app = new ActiveXComponent(type); // System.out.println("启动word"); // // 要转换的word文件 // // HTML文件 // try { // app.setProperty("Visible", new Variant(false)); // // 设置word不可见 // Dispatch docs = app.getProperty(property).toDispatch(); // Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new // Object[]{paths, new Variant(false), new Variant(true)}, new // int[1]).toDispatch(); // // 打开word文件 // Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{tpFile, // variant}, new int[1]); // // 作为html格式保存到临时文件 // Variant f = new Variant(false); // Dispatch.call(doc, "Close", f); // } catch (Exception e) { // e.printStackTrace(); // } finally { // app.invoke("Quit", new Variant[]{}); // } // System.out.println("转化完毕!"); // } // System.out.println("开始读取"); // FileParseImpl imp = new FileParseImpl(); // return imp.readHtml(tpFile); // } private boolean isTrimEmpty(String astr) { return (null == astr) || (astr.length() == 0) || isBlank(astr.trim()); } /** * 字符串是否为空:null或者长度为0. * * @param astr 源字符串. * @return boolean */ private static boolean isBlank(String astr) { return (null == astr) || (astr.length() == 0); } }
所用到的包在附近中。
- fileParse.rar (6.7 MB)
- 下载次数: 14
相关推荐
* 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws ...
java配置文件修改读取工具类,包括yml和properties类型文件
提供java中对文件类的各种基本操作,主要包括获取文件的后缀名称,读取文件内容,写入文件内容,拷贝文件,将文件转换为二进制数组等操作,转换为Blob格式类等操作
POI,读取文件工具类和笔记
读取xls表格文件工具类,很简单的一个类。直接使用即可,
//以字符为单位读取文件,常用于读文本,数字等类型的文件 public static void readFileByChars(String fileName)……; //以字符为单位读取文件,常用于读文本,数字等类型的文件 public static void ...
包括: 1)建立,读取,删除文件等基础方法 2)获得读取文件的流,并可以设置读取文件的编码方式,从而有效解决读文件中出现的乱码问题。 3)获得写文件的流,并可以设置写文件的编码方式。 4)其他...
通过文件头识别文件类型,图片大小 支持格式: "JPEG", "GIF", "PNG", "BMP", "PCX", "IFF", "RAS", "PBM", "PGM", "PPM", "PSD", "SWF", "KRO
一些常用的java工具类:Date和String类型互转,获取想要格式的String类型的日期时间、java导出数据到Excel、http文件下载、HMAC-MD5加密、3DES加密、MD5加密、读写txt文件、zip解压缩文件、文件夹等
sqlserver中提示: 无法使用闩锁类型 SH 读取并闩锁页;由于不可修复的错误,CHECK 语句已终止;CHECKDB 发现有 0 个分配错误和 2 个一致性错误与任何单个的对象都没有关联;CHECKDB 在数据库 中发现 0 个分配错误和...
可以获得,当前文件的sheet列表,读取数据并封装到List 1.使用poi excel jar 包 2.excel 表格需要有表头,名称可以是表字段的,也可以是java 实体属性的 3.最终封装的结果是java.util.List<?> 结果 4.有日期类型...
文件工具类,可用于读写文件及对文件进行操作。如: readFile(String filePath) 读文件 writeFile(String filePath, String content, boolean append) 写文件 getFileSize(String path) 得到文件大小 deleteFile...
银行的机构号 银行卡的卡的类型 银行卡的卡种 工具类是由java 编写 其他的开发语言 也可以参考 包含了4800 +种卡种 自己测试了 还是比较全的 比支付宝提供的接口返回的卡种还要全 读取银行数据文件的时候 ...
工具类包 Assert提供对数组、集合、字符串的合法校验静态方法 BeanHelper提供了bean类与map的互相转换 PropertiesUtil实现对properties文件的读与写,参数文件名不包括.properties SysConfig实现对指定系统资源文件...
Java读写修改Property文件,一个已经可以使用的工具类。注意修改里面的property文件的路径。
各种各样额java文件处理工具类,包涵几乎所有对File的操作
本篇介绍Android获取本机各种类型文件的方法,已经封装成工具类,末尾有源码下载地址。 提示 获取音乐、视频、图片、文档等文件是需要有读取SD卡的权限的,如果是6.0以下的系统,则直接在清单文件中声明SD卡读取权限...
乐易佳文件恢复软件工具是一款功能强大的数据恢复软件,软件支持硬盘、U盘、内存卡等多种设备可以有效解决各种硬盘发生故障而不能读取数据,或是人为操作失误及病毒侵袭造成硬盘分区或是数据丢失问题!软件界面美观...
基于Java语言的NetCDF文件解析,使用了netcdf4和opencsv两个库,最终将解析后的数据导出为CSV,附依赖文件、源代码及一个测试数据样例。 可结合本人的博客...