欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

使用java NIO及高速缓冲区写入文件过程解析

时间:2021-03-24 10:29:12|栏目:JAVA代码|点击:

这篇文章主要介绍了使用java NIO及高速缓冲区写入文件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

byte[] bytes = Files.readAllBytes(Paths.get("E:\\pdf\\aaa\\html\\text.txt").normalize());
    String text = IOUtils.toString(bytes);
    String xml = text.substring(text.indexOf("<tbody>"));
    InputSource inputXML = new InputSource( new StringReader( xml ) );
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xPath.evaluate("/tbody/tr", inputXML, XPathConstants.NODESET);
    int length = nodes.getLength();
    Path file = Paths.get("E:\\pdf\\aaa\\html\\out.txt");
    try (BufferedWriter writer = Files.newBufferedWriter(file, Charset.defaultCharset(), StandardOpenOption.CREATE)) {
      for (int i = 0; i < length; i++) {
        Node node = nodes.item(i);
        NodeList childList = (NodeList) xPath.evaluate("td", node, XPathConstants.NODESET);
        for (int j = 0; j < childList.getLength(); j++) {
          Node child = childList.item(j);
          String content = child.getTextContent();
          //System.out.print(content);
          writer.write(content);
          if (j <childList.getLength() - 1) {
            writer.write("\t");
          }
        }
        writer.newLine();
      }
    }

text.txt内容

输出内容:

上一篇:java生成图片验证码示例程序

栏    目:JAVA代码

下一篇:Java受检异常的一些思考

本文标题:使用java NIO及高速缓冲区写入文件过程解析

本文地址:http://www.codeinn.net/misctech/86890.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有