解析一个文件夹所有文件的中文,并输出到某一文本文档中

public class DoGetChinese{ 
public static void main(String[] args) {
            String src = "D:/ab.txt";
            String res = "D:\\static";
            File srcFile = new File(src);
            File resFile = new File(res);
            System.out.println(dowork(srcFile,resFile));
        }

        public static boolean dowork(File srcFile,File resFile){
            if(resFile.isDirectory()){
                File[] files = scanFile(resFile);
                for(File f:files){
                    dowork(srcFile,f);
                }
            }else{
                System.out.println("开始解析 "+resFile.getName()+"文件");
                analysefile(srcFile,resFile);
            }

            return true;
        }

        public static File[] scanFile(File file){
            return file.listFiles();
        }

        public static void analysefile(File srcfile,File resFile){
            try(
          //这里建立流输入流时注意设置源文件的编码格式,默认为utf-8
          FileReader fr = new FileReader(resFile); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(srcfile,true); PrintWriter pw = new PrintWriter(fw); ){ List<String> words; while (true) { // 一次读一行 String line = br.readLine(); if (null == line) break; words = getChinese(line); for(String s:words){ pw.println(s); } } }catch (Exception e){ e.printStackTrace(); } } /*1、至少匹配一个汉字的写法。 2、这两个unicode值正好是Unicode表中的汉字的头和尾。 3、"[]"代表里边的值出现一个就可以,后边的“+”代表至少出现1次,合起来即至少匹配一个汉字。 */ public static List<String> getChinese(String paramValue) { String regex = "([\u4e00-\u9fa5]+)"; String str = ""; Matcher matcher = Pattern.compile(regex).matcher(paramValue); List<String> result = new ArrayList<>(); while (matcher.find()) { result.add(matcher.group(0)); } return result; } }

 

转载于:https://www.cnblogs.com/haoyangblog/p/7575237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值