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) {
}
}