java中无法解析为类型_java无法解析导入的包

java中无法解析为类型_java无法解析导入的包1、了解.odt文件.odt文件是openoffice软件产生的文档格式,可以直接用office打开,这其实就是一个压缩包,可以使用解压软件打开,里面有一个content.xml文件,这个文件内有<text:p>标签,标签内就是展示出来的内容。2、解压文件上面说了.odt文件就是一个压缩包,所以可以直接用解压缩包的方式解压,/***@paramfilePath源文件路径*/publicvoidparseFile(Stringfi…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1、了解.odt文件

        .odt文件是openoffice软件产生的文档格式,可以直接用office打开,这其实就是一个压缩包,可以使用解压软件打开,里面有一个content.xml文件,这个文件内有<text:p>标签,标签内就是展示出来的内容。

2、解压文件

        上面说了.odt文件就是一个压缩包,所以可以直接用解压缩包的方式解压,

/**
* @param filePath 源文件路径
*/
public void parseFile(String filePath) throws IOException{
    File file = new File(filePath);
    // 原文件是否存在
    if(!file.exists()){
        throw new FileNotFoundException("文件不存在");
    }
    // 解压到源文件的同级目录下
    String parent = file.getParent();
    File file1 = new File(parent);
    odtUncompress(file, file1);
}

/**
     * zip文件解压
     * @param inputFile  待解压文件夹/文件
     * @param destDirPath  解压路径
     */
    public static void odtUncompress(String inputFile,String destDirPath) throws Exception {
        File srcFile = new File(inputFile);//获取当前压缩文件
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            throw new Exception(srcFile.getPath() + "所指文件不存在");
        }
        ZipFile zipFile = new ZipFile(srcFile);//创建压缩文件对象
        //开始解压
        Enumeration<?> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            // 如果是文件夹,就创建个文件夹
            if (entry.isDirectory()) {
                String dirPath = destDirPath + "/" + entry.getName();
                srcFile.mkdirs();
            } else {
                // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                File targetFile = new File(destDirPath + "/" + entry.getName());
                // 保证这个文件的父文件夹必须要存在
                if (!targetFile.getParentFile().exists()) {
                    targetFile.getParentFile().mkdirs();
                }
                targetFile.createNewFile();
                // 将压缩文件内容写入到这个文件中
                InputStream is = zipFile.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(targetFile);
                int len;
                byte[] buf = new byte[1024];
                while ((len = is.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }
                fos.close();
                is.close();
            }
        }
    }

3、获取xml文件内容

        因为我是需要修改xml文件内容,所以我还是从.odt文件入手,直接拿到xml文件

// 记录标签内容
private static String str = "";

/**
* .odt文件的路径
* @param srcFile
* @throws Exception
*/
public void originalContent(String srcFile) throws Exception {
    ZipFile zipFile = new ZipFile(srcFile);
    Enumeration entries = zipFile.entries();
    ZipEntry entry;
    org.w3c.dom.Document doc = null;
    while (entries.hasMoreElements()) {
        entry = (ZipEntry) entries.nextElement();
        // 只操作xml文件
        if (entry.getName().equals("content.xml")) {
            // 构建文档
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder docBuilder = domFactory.newDocumentBuilder();
            doc = docBuilder.parse(zipFile.getInputStream(entry));

            // 获取节点
            NodeList list = doc.getElementsByTagName("text:p");

            for (int a = 0; a < list.getLength(); a++) {
                Node node =list.item(a);
                // 递归获取标签内容
                getText(node);
                System.out.println(str);
                // 清空数据,记录下个标签的内容
                str = "";
            }
        }
    }
}


// 递归获取子标签的内容
    private static void getText(org.w3c.dom.Node node) {
        if (node.getChildNodes().getLength() > 1) {
            NodeList childNodes = node.getChildNodes();
            for (int a = 0; a < childNodes.getLength(); a++) {
                getText(node.getChildNodes().item(a));
            }
        } else {
            if (node.getNodeValue() != null) {
                // str用来连接标签内容 用static修饰
                str = str + node.getNodeValue();
            }
            if (node.getFirstChild() != null) {
                str = str + node.getFirstChild().getNodeValue();
            }
        }
    }

至于将解压后的文件在压缩回去,也是和普通的文件压缩一样的,大家可以去看一下别人的,我就不写了,只要将后缀改成.odt就可以了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197436.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vlan教学_不同的vlan转发降低速度

    vlan教学_不同的vlan转发降低速度https://www.cnblogs.com/clover-toeic/p/3741115.html

    2022年8月10日
  • 迅雷的远程下载_迅雷下载网页

    迅雷的远程下载_迅雷下载网页-100网络连接超时点击重试或暂停/开启任务尝试解决。3设备响应超时请彻底删除该任务,重新下载。4服务器内部错误请彻底删除该任务,重新下载。5读写文件失败1:检测磁盘剩余空间;2:检测磁盘是否可读写;3:拔下硬盘连接PC修复硬盘文件,然后重新接上硬盘;4:上述方法无效的情况下,请联系客服28设备空间不足1:检测磁盘剩余空间;2:检测磁盘是否可读写;3:拔下硬盘连接PC修复硬盘文件,然后重新接上硬盘…

    2022年10月23日
  • 失去焦点和获得焦点发生事件(js)「建议收藏」

    失去焦点和获得焦点发生事件(js)「建议收藏」失去焦点:onblur=”hanshu(this)”获得焦点:onfocus=”hanshu(this)”{     alert(‘请确认您输入格式是否正确!’);   }//函数名:chksafe//功能介绍:检查是否含有,//,///参数说明:要检查的字符串//返回值:0:是 1:不是functionchksafe(a)

    2022年6月30日
  • linux下安装pip3(ubuntu怎么安装pip)

    首先下载文件:复制执行即可wgethttps://bootstrap.pypa.io/get-pip.py–no-check-certificate 安装:pythonget-pip.py 

    2022年4月17日
  • 查看CentOS的系统版本[通俗易懂]

    查看CentOS的系统版本[通俗易懂]记录一下以免忘记:CentOS查看版本有如下:cat/etc/redhat-releaseuname-acat/etc/issue#这个好像7以上看不了cat/proc/version#这个是和内核有关的数据getconfLONG_BITorgetconfWORD_BIT#查看版本位数是64位还是32位…

    2022年6月24日
  • python中encode和decode的区别_python rindex

    python中encode和decode的区别_python rindex1.基本语法1.encode()和decode()都是字符串的函数decode解码encode编码str———>str(Unicode,byte类型)———>str2.decode()与encode()方法可以接受参数,其声明分别为:其中的encoding是指在解码编码过程中使用的编码(此…

    2022年10月6日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号