java 按行读取文件并计算每行字符串重复率

  • Post author:
  • Post category:java


public static void main(String[] args) {
        try {
            File myFile = new File("C:\\Users\\wcxjo\\Desktop\\zc\\zc2.txt");//通过字符串创建File类型对象,指向该字符串路径下的文件
            List<String> txtList = new ArrayList<>();
            if (myFile.isFile() && myFile.exists()) { //判断文件是否存在

                InputStreamReader Reader = new InputStreamReader(new FileInputStream(myFile), "UTF-8");
                //考虑到编码格式,new FileInputStream(myFile)文件字节输入流,以字节为单位对文件中的数据进行读取
                //new InputStreamReader(FileInputStream a, "编码类型")
                //将文件字节输入流转换为文件字符输入流并给定编码格式

                BufferedReader bufferedReader = new BufferedReader(Reader);
                //BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
                //通过BuffereReader包装实现高效读取

                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    //buffereReader.readLine()按行读取写成字符串
                    txtList.add(lineTxt);
                    System.out.println(lineTxt);
                }

                Reader.close();

            } else {

                System.out.println("找不到指定的文件");

            }
            int a = 0;
            Map<String,String> repeat = new HashMap<String,String>();
            File f=new File("C:\\Users\\wcxjo\\Desktop\\zc\\zc2222.txt");
            FileOutputStream fos1=new FileOutputStream(f);
            OutputStreamWriter dos1=new OutputStreamWriter(fos1);


            for(String str : txtList){

                for(String str2 : txtList){
                    if(str.equals(str2)){
                        continue;
                    }
                    if(MyUtils.getSimilarityRatio(str,str2) > 80 ){
                        if(repeat.containsKey(str)|| repeat.containsKey(str2)){
                            continue;
                        }
                        repeat.put(str2,str2);
                        repeat.put(str,str);
                        a++;
                        System.out.println("**********************************************");
                        System.out.println(str);
                        System.out.println(str2);
                        System.out.println("**********************************************");
                        dos1.write("****************");
                        dos1.write(System.getProperty("line.separator"));
                        dos1.write(str);
                        dos1.write(System.getProperty("line.separator"));
                        dos1.write(str2);
                        dos1.write(System.getProperty("line.separator"));
                        dos1.write("****************");
                        dos1.write(System.getProperty("line.separator"));
                    }

                }

            }
            dos1.close();
            System.out.println("重复总数:"+a);
        } catch (Exception e) {

            System.out.println("读取文件内容出错");

            e.printStackTrace();
        }
    }

工具类,字符串重复计算

public class MyUtils {

    public static float getSimilarityRatio(String str, String target) {
        int d[][]; // 矩阵
        int n = str.length();
        int m = target.length();
        int i; // 遍历str的
        int j; // 遍历target的
        char ch1; // str的
        char ch2; // target的
        int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
        if (n == 0 || m == 0) {
            return 0;
        }
        d = new int[n + 1][m + 1];
        for (i = 0; i <= n; i++) { // 初始化第一列
            d[i][0] = i;
        }

        for (j = 0; j <= m; j++) { // 初始化第一行
            d[0][j] = j;
        }

        for (i = 1; i <= n; i++) { // 遍历str
            ch1 = str.charAt(i - 1);
            // 去匹配target
            for (j = 1; j <= m; j++) {
                ch2 = target.charAt(j - 1);
                if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
                    temp = 0;
                } else {
                    temp = 1;
                }
                // 左边+1,上边+1, 左上角+temp取最小
                d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + temp);
            }
        }

        return (1 - (float) d[n][m] / Math.max(str.length(), target.length())) * 100F;
    }

}



版权声明:本文为wcx18526659867原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。