栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用java遍历所有文件夹,将word文件转换为txt格式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用java遍历所有文件夹,将word文件转换为txt格式

用Java代码,遍历文件夹及子文件夹,将其中的doc和docx文件批量转换为txt格式 文件夹结构 bbb文件夹

 ccc文件夹

加几个除word外的干扰项

 ddd文件夹

 依赖
    

        
            com.jacob
            jacob
            1.19
            system
            ${basedir}/src/main/resources/lib/jacob.jar
        

        
            org.apache.poi
            poi
            3.10-FINAL
        

        
            org.apache.poi
            poi
            3.10.1
        

        
            org.apache.poi
            poi-ooxml-schemas
            3.10.1
        

        
            org.apache.poi
            poi-ooxml
            3.10.1
        

        
            org.apache.poi
            poi-scratchpad
            3.9
        

        
            fr.opensagres.xdocreport
            xdocreport
            2.0.1
        

        
            fr.opensagres.xdocreport
              
            fr.opensagres.xdocreport.document
              
            2.0.1
        

        
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.core
            1.0.6
        

        
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.pdf
            1.0.6
        

        
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.xhtml
            1.0.6
        

    
代码 主方法

这里需要说明的是,同一文件夹中可能会存在A.doc和A.docx这样的情况,当转换为A.txt时后转换的会覆盖掉先前转换的,所以定义了两个路径输出到不同文件夹中来解决(当然也可以在文件名后加随机数等方法),不过这里就先不考虑这个问题,暂时写一个路径,可以根据自己需求修改

    public static void main(String[] args) throws Exception {
        try {
//            定义word文件所在路径
            String path="F:\bbb";
//          定义输出txt文件所在路径
            String outdocPath = "F:\zzz";
            String outdocxPath = "F:\zzz";
            path = URLDecoder.decode(path, "UTF-8");
//            调用方法,遍历文件夹
            linkedList files = EveryFile.GetDirectory(path);
            for (int i = 0; i < files.size(); i++) {
//                word文件所在路径
                String filesName = String.valueOf(files.get(i));
//                word文件名
                String fileName=files.get(i).getName();
//                调用方法,进行转换
                WordToTxt.word2txt(filesName,fileName,outdocPath,outdocxPath);
            }
            System.out.println("转换完毕");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
遍历文件夹代码
    
    public static linkedList GetDirectory(String path) {
        File file = new File(path);
        linkedList Dirlist = new linkedList(); // 保存待遍历文件夹的列表
        linkedList fileList = new linkedList();
        GetoneDir(file, Dirlist, fileList);// 调用遍历文件夹根目录文件的方法
        File tmp;
        while (!Dirlist.isEmpty()) {
            tmp = (File) Dirlist.removeFirst();// 从文件夹列表中删除第一个文件夹,并返回该文件夹赋给tmp变量
            // 遍历这个文件夹下的所有文件,并把
            GetoneDir(tmp, Dirlist, fileList);

        }
        return fileList;
    }

    // 遍历指定文件夹根目录下的文件
    private static void GetoneDir(File file, linkedList Dirlist,
                                  linkedList fileList) {
        // 每个文件夹遍历都会调用该方法
        File[] files = file.listFiles();

        if (files == null || files.length == 0) {
            return;
        }
        for (File f : files) {
            if (f.isDirectory()) {
                Dirlist.add(f);
            } else {
                // 这里列出当前文件夹根目录下的所有文件,并添加到fileList列表中
                fileList.add(f);
                // System.out.println("file==>" + f);

            }
        }
    }
word转txt代码
    
    public static void word2txt(String filesName, String fileName, String outdocPath, String outdocxPath) throws Exception {

        String fileType = new String("");
        fileType = filesName.substring(filesName.length() - 4, filesName.length());

        if (fileType.equals("docx")) {
//            要转换的文档全路径
            String docxPath = filesName;
//            转换后的文档全路径
            String docxtotxtPath = outdocxPath + "/" + fileName.substring(0, fileName.length() - 5) + ".txt";

            //得到.docx文件提取器
            XWPFWordExtractor docx = new XWPFWordExtractor(POIXMLdocument.openPackage(docxPath));
            //提取.docx正文文本
            String text = docx.getText();

            FileWriter writer = new FileWriter(docxtotxtPath);
            writer.write(text);
            writer.close();

        } else if (fileType.equals(".doc")) {
//            要转换的文档全路径
            String docPath = filesName;
//            转换后的文档全路径
            String doctotxtPath = outdocPath + "/" + fileName.substring(0, fileName.length() - 4) + ".txt";
            InputStream is = new FileInputStream(docPath);
            HWPFdocument worddocument = new HWPFdocument(is);
            WordToTextConverter converter = new WordToTextConverter(documentBuilderFactory.newInstance().newdocumentBuilder().newdocument());

            //对HWPFdocument进行转换
            converter.processdocument(worddocument);
            Writer writer = new FileWriter(new File(doctotxtPath));
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            //是否添加空格
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "text");
            transformer.transform(
                    new DOMSource(converter.getdocument()),
                    new StreamResult(writer));
        }
    }
运行结果

 后记

word文件中的图片转换到txt后将不会保留。

代码一定存在可以优化的地方,一是水品有限,二是手头还有其他的事要忙,目前这些代码满足需要,所以暂时没有修改。各位大佬使用的时候根据自己需要修改,不足之处欢迎批评指正。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/355840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号