2019-05-31

Java查找文件中的重复行

public static void main(String[] args) {

        ArrayList<String> arrayList = new ArrayList<>(10000);

        List<String> repeatList = new ArrayList<String>(10000);

        String line="";

        File file =new File("D:/companyFile/TestToExcel/jhpAnalyze/No.txt");

        File fileOutPut =new File("D:/companyFile/TestToExcel/jhpAnalyze/repeat.txt");

        FileInputStream fis;InputStreamReader isr;BufferedReader br;

        try {

        fis = new FileInputStream(file);

        isr = new InputStreamReader(fis, "UTF-8");

        br = new BufferedReader(isr);

        while((line=br.readLine())!=null){

        String[] sArray = line.split(",");

                for (String s : sArray) {

                arrayList.add(s);

                }

        }

        System.out.println(arrayList.size());

                br.close();isr.close();fis.close();

                int lengh = arrayList.size();

                for(int i=0;i<lengh-1;i++){

                for(int j=lengh-1;j>i;j--){

                if(arrayList.get(i).equals(arrayList.get(j))){

                repeatList.add(arrayList.get(j));

                }

                }

                }

                for(int k=0;k<repeatList.size();k++){

                FileOutputStream fos = new FileOutputStream(fileOutPut,true);

                    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

                    BufferedWriter bw = new BufferedWriter(osw);

                    bw.write(repeatList.get(k));

                    bw.newLine();

                    bw.close();

                    osw.close();

                    fos.close();

                }

    } catch (Exception e) {

    }

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Java去除文件中的重复行 public static void main(String[] args) thro...
    hotchen阅读 214评论 0 0
  • Java实现复制文件夹中的文件内容到另一个文件夹中 public static void main(String[...
    hotchen阅读 205评论 0 0
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,421评论 0 4
  • chinadaily.com.cn 2019-05-30 15:30 8:45 – 9: 20, 35 min >...
    Tramereiisun阅读 308评论 0 0
  • 遥想1999年刚入大学,高中时期的学霸在清华园,提笔疾书,时常鼓励,并告知其梦想为MIT,19年后,他在遥远的...
    autumnleaf阅读 185评论 0 0