读取docx文档中的姓名并重命名文档

1 篇文章 0 订阅

需要用到的框架,poi,把下载的zip内所有的jar引用到项目中。

代码如下:

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlException;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
//        XWPFWordExtractor wordExtractor = new XWPFWordExtractor();
        File[] fs;
        File outDir;

        /*当file不存在时都是false
        File tmp = new File("/Users/ge.su/Desktop/2018年度工作互评表.docx");
        System.out.println("is dir "+tmp.isDirectory());
        System.out.println("is file "+tmp.isFile());
        */

//        获取到的路径带..,getAbsolutePath也带,getCanonicalPath会转换成/sdcard/test
//        File tp = new File("/sdcard/Download/../test");
//        System.out.println(tp.getParent()+"="+tp.getParentFile().getAbsolutePath()+"="+tp.getCanonicalPath());

        System.out.println(args.length);

        if (args.length < 1) {
            System.out.println("请输入要转换名字的目录或单个文件!参考如下示例:");
            System.out.println("xx.jar /Users/ge.su/Desktop/2018年度工作互评表.docx");
            System.out.println("也可以指定改名后保存的目录!参考如下示例:");
            System.out.println("xx.jar /Users/ge.su/Desktop/2018年度工作互评表.docx /Users/ge.su/Desktop/new");
            return;
        } else if (args.length > 2) {
            System.out.println("太多的参数");
            return;
        } else {
            String in = args[0];
            File inFile = new File(in);
            if (!inFile.exists()) {
                System.out.println(in+" 不存在!");
                return;
            } else if (!inFile.canRead()) {
                System.out.println(in+" 不可读!");
                return;
            } else if (inFile.isDirectory()) {
                System.out.println(in + " 为目录!");
                File[] files = inFile.listFiles();
                List<File> list = new ArrayList<>();
                if (files != null && files.length > 0) {
                    System.out.println("拥有" + files.length + "个文件!");
                    for (int i = 0; i < files.length; i++) {
                        if (files[i].isDirectory()) {
                            System.out.println(files[i] + " 为目录,暂不处理!");
                        } else {
                            list.add(files[i]);
                        }
                    }

//                    fs = (File[]) list.toArray();
                    fs = new File[list.size()];
                    for (int i = 0; i < list.size(); i++) {
                        fs[i] = list.get(i);
                    }

                } else {
                    System.out.println(inFile+" 为空目录!");
                    return;
                }
//                fs = files;
            } else {
                System.out.println(in + " 为文件!");
                fs = new File[]{inFile};
            }

            if (args.length == 2) {
                String out = args[1];
                File outFile = new File(out);
                if (outFile.isFile() && outFile.exists()) {
                    System.out.println(out + " 是文件,请选择目录!");
                    return;
                }

                if (!outFile.exists()) {
                    boolean mkdirs = outFile.mkdirs();
                    if (!mkdirs) {
                        System.out.println("创建" + out + " 目录失败");
                        return;
                    }
                }

                outDir = outFile;

            } else {
                outDir = inFile.isFile() ? inFile.getParentFile() : inFile;
            }

        }

//        String file = "/Users/ge.su/Desktop/2018年度工作互评表.docx";

        for (int i = 0; i < fs.length; i++) {
            String file = fs[i].getAbsolutePath();
            XWPFDocument docx = null;
            BufferedReader br = null;
            boolean findName = false;
            StringBuffer sb = new StringBuffer();
            String[] match = new String[]{"姓名:", "姓名:"};
            try {
                docx = new XWPFDocument(
                        new FileInputStream(file));

                //using XWPFWordExtractor Class
                XWPFWordExtractor we = new XWPFWordExtractor(docx);
                String text = we.getText();

                br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(text.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
                String line;

                while ((line = br.readLine()) != null) {
    //            if(!line.trim().equals("")){
    //                line="<br>"+line;//每行可以做加工
    //                sb.append(line+"\r\n");
    //            }
    //
                    line = line.trim();

                    int who = -1;
                    /*
                    if (line.startsWith(match[0])) {
                        who = 0;
                    } else if (line.startsWith(match[1])) {
                        who = 1;
                    }
                    */

                    String trim = null;
                    for (int y = 0; y < match.length; y++) {
                        if (line.startsWith(match[y])) {
                            who = y;
                            System.out.println(line);
                            trim = line.trim();
                            break;
                        }
                    }

                    if (who != -1) {
                        trim = subStr(trim, match[who].length());
                        System.out.println(trim);
                        File source = new File(file);
    //                    File parentFile = source.getParentFile();
                        File out = new File(outDir, "2018年度工作互评表." + trim + ".docx");
                        boolean b = source.renameTo(out);
                        if (b) {
                            System.out.println(file + " -> " + out.getAbsolutePath());
                        } else {
                            System.out.println(file + " 重命名失败,检测目录是否有权限!");
                        }
                        findName = true;
                        break;
                    } else {
                        /*
                        for (String s : match) {
                            sb.append(s).append(";");
                        }
                        System.out.println(file+" 内未发现 "+sb.toString());
                        */
                    }

                    /*
                    if (line.startsWith("姓名:")) {
                        System.out.println(line);
                        String trim = line.trim();
                        trim = trim.substring("姓名:".length());
                        System.out.println(trim);
                        File source = new File(file);
                        File parentFile = source.getParentFile();
                        source.renameTo(new File(parentFile, "2018年度工作互评表." + trim + ".docx"));
                    } else if (line.startsWith("姓名:")) {
                        System.out.println(line);
                        String trim = line.trim();
                        trim = trim.substring("姓名:".length());
                        System.out.println(trim);
                        File source = new File(file);
                        File parentFile = source.getParentFile();
                        source.renameTo(new File(parentFile, "2018年度工作互评表." + trim + ".docx"));
                    }
                    */
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (!findName) {
                    for (String s : match) {
                        sb.append(s).append(";");
                    }
                    System.out.println(file+" 内未发现 "+sb.toString());
                }
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

//        System.out.println(we.getText());
        }
    }

    private static String subStr(String str, int index) {
        return str.substring(index);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值