本帖最后由 栗学魁 于 2012-8-29 22:45 编辑
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- /*
- * 查找文件里面重复的行,并输出全部重复的行号。
- *
- *
- */
- public class Test6 {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new FileReader("Test5.txt"));
- String strline = "";
- int row = 1;
- HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
- List<Integer> rowFound;
- while ((strline = reader.readLine()) != null) {
- // 尝试查找以前是否存在
- rowFound = map.get(strline);
- // 找到
- if (rowFound != null) {
- // 保存新行好
- rowFound.add(row);
- } else {
- // 新行,没有重复的
- List<Integer> list = new LinkedList<Integer>();
- list.add(row);
- map.put(strline, list);
- }
- row++;
- }
- reader.close();
- // 开始显示长度超过一个的行数
- Iterator<String> it = map.keySet().iterator();
- String line;
- while (it.hasNext()) {
- line = it.next();
- rowFound = map.get(line);
- if (rowFound.size() > 1) {
- System.out.print(line);
- // System.out.println(line);//如是不加Ln就不换行,但是加了会在输出结果前多一行,哪里错了?
- for (Integer rowNum : rowFound) {
- System.out.print("," + rowNum);
- }
- }
- }
- }
- }
复制代码 哪写错了
|