读取java文件里面所有中文

最近做国际化,提取代码的中文出来

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname Process
 * @Description
 * @Created by DELL
 */
public class Process {

    private PrintWriter output = null;

    /**
     * 看下自己IDEA的文件格式
     */
    private String charset = "UTF-8";

    public Process(PrintWriter output, String charset) {
        this.output = output;
        this.charset = charset;
    }
    public List<String> getWord(String names){
        List<String> word = new ArrayList<>();
        List<String> strings = Arrays.asList(names.split("\""));
        for (String string : strings) {
            Pattern p= Pattern.compile("[\u4e00-\u9fa5]");
            Matcher m = p.matcher(string);
            if(m.find()){
                word.add(string);
            }
        }
        return word;
    }
    public void readTxt(File tempFile) throws IOException {
        System.out.println(tempFile.getName());
        String packageName = Arrays.asList(tempFile.getPath().substring(tempFile.getPath().indexOf("DQMS") +5).split("\\"+File.separator)).get(0);
        if (tempFile.getName().indexOf(".java")>0){
            System.out.println("#" + tempFile.getName() + "\n");
            String fileName = tempFile.getName().substring(0,tempFile.getName().lastIndexOf("."));
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile), "UTF-8"));

            String tempString = "";
            Pattern p= Pattern.compile("[\u4e00-\u9fa5]");
            /**
             * 行號
             */
            int i = 1;
            int j = 001;
            while ((tempString = reader.readLine()) != null) {
                tempString = tempString.trim();
                Matcher m = p.matcher(tempString);
                if (tempString.indexOf("*")<0&&tempString.indexOf("//")<0&&
                        tempString.indexOf("@ResourcePermissions")<0&&m.find()
                        &&tempString.indexOf("@OptLogAnnotation")<0&&tempString.indexOf("logger.")<0
                        &&tempString.indexOf("<!--")<0){
                    List<String> word = getWord(tempString);
                    for (String s : word) {
                        output.write(tempFile.getName() +"@@@"+ i +"@@@"+tempString +"@@@" +packageName+"_"+fileName +"_"+String.format("%04d" ,j) +"@@@"+s +"\n");
                    }
                    j++;
                }
                i ++;
            }
            reader.close();
        }


    }

    public void readDir(String folder) throws IOException {

        File dir = new File(folder);

        if (dir.isDirectory()) {

            System.out.println("#Dir#" + dir.getName() + "\n");

            //output.write("#Dir#" + dir.getName() + "\n");

            String[] children = dir.list();

            for (int i = 0; i<children.length;i++){

                File tempFile = new File(dir, children[i]);
                if (tempFile.isDirectory()) {
                    readDir(tempFile.getPath());
                } else {
                    readTxt(tempFile);
                }

            }

        }

    }
}
public class Test {

//输出文件路径

    public static String outFile = "D:/ceshi.txt";

//输入文件夹路径

    public static String inFolder = "D:\\work";

    public static String charset = "UTF-8";

    public static void main(String[] args) throws IOException {
        String tempString = "import java.io.FileWriter;";
        tempString = "3试试";
        Pattern p= Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(tempString);
        if (m.find()){
            System.out.println(tempString);
        }
        PrintWriter output = new PrintWriter(new FileWriter(new File(outFile)));

        Process process = new Process(output, charset);

        process.readDir(inFolder);

        output.close();

    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
读取gz文件里面文件,可以使用Java的GZIPInputStream类和ZipInputStream类。以下是一个简单的示例代码: ```java import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.zip.GZIPInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class GzFileReader { public static void main(String[] args) { try { // 创建GZIPInputStream对象 GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream("file.gz")); // 创建ZipInputStream对象 ZipInputStream zipInputStream = new ZipInputStream(gzipInputStream); // 迭代ZipEntry对象 ZipEntry entry = zipInputStream.getNextEntry(); while (entry != null) { // 只处理文件 if (!entry.isDirectory()) { // 获取文件名 String fileName = entry.getName(); // 创建BufferedReader对象 BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream)); // 读取文件内容 String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 关闭流 reader.close(); } // 获取下一个ZipEntry对象 entry = zipInputStream.getNextEntry(); } // 关闭流 zipInputStream.close(); gzipInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们使用GZIPInputStream来读取名为“file.gz”的gz文件,然后使用ZipInputStream来读取里面文件。我们通过迭代ZipEntry对象来获取每个文件的内容,并逐行打印到控制台。最后,我们关闭了流。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值