求助:使用BufferedReader与while循环找出三个文件的公共行
解决多文本文件公共行查找问题(基于BufferedReader和while循环)
我来帮你搞定这个问题!你现在的核心问题是第二个文件仅处理了第一行就停止,而且需要扩展到三个文件的公共行查找,咱们一步步来实现:
第一步:先搞定两个文件的公共行查找
你的问题根源应该是第二个文件的readLine()没有放在while循环里,导致只读取了第一行。正确的逻辑是先把第一个文件的所有行存入一个高效查找的集合(比如HashSet),然后逐行读取第二个文件的内容,比对是否存在于集合中。
完整代码示例:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashSet; import java.util.Set; public class CommonLineFinder { public static void main(String[] args) { // 定义文件路径,可根据实际修改 String file1Path = "file1.txt"; String file2Path = "file2.txt"; Set<String> file1Lines = new HashSet<>(); // 读取第一个文件的所有行到集合 try (BufferedReader br1 = new BufferedReader(new FileReader(file1Path))) { String line; // while循环逐行读取,直到line为null(文件结束) while ((line = br1.readLine()) != null) { file1Lines.add(line.trim()); // trim避免空格导致的不匹配 } } catch (IOException e) { System.err.println("读取第一个文件出错: " + e.getMessage()); return; } // 读取第二个文件,逐行比对公共行 try (BufferedReader br2 = new BufferedReader(new FileReader(file2Path))) { String line; while ((line = br2.readLine()) != null) { String trimmedLine = line.trim(); if (file1Lines.contains(trimmedLine)) { System.out.println("公共行: " + trimmedLine); } } } catch (IOException e) { System.err.println("读取第二个文件出错: " + e.getMessage()); } } }
关键说明:
- 使用
try-with-resources语法,无需手动关闭BufferedReader,JVM会自动处理,避免资源泄漏。 while ((line = br.readLine()) != null)是正确的逐行读取写法,每次循环都会读取下一行,直到文件末尾。- 调用
trim()是为了避免行首行尾的空格导致原本相同的行被判定为不同,可根据需求决定是否保留。
第二步:扩展到三个文件的公共行查找
逻辑很简单:先找出前两个文件的公共行,存入新集合,再用第三个文件的每一行和这个集合比对,存在的就是三个文件的公共行。
完整代码示例:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashSet; import java.util.Set; public class ThreeFileCommonLineFinder { public static void main(String[] args) { String file1Path = "file1.txt"; String file2Path = "file2.txt"; String file3Path = "file3.txt"; // 第一步:获取前两个文件的公共行 Set<String> commonLinesOfTwo = getCommonLines(file1Path, file2Path); if (commonLinesOfTwo.isEmpty()) { System.out.println("前两个文件没有公共行"); return; } // 第二步:读取第三个文件,比对公共行 try (BufferedReader br3 = new BufferedReader(new FileReader(file3Path))) { String line; while ((line = br3.readLine()) != null) { String trimmedLine = line.trim(); if (commonLinesOfTwo.contains(trimmedLine)) { System.out.println("三个文件的公共行: " + trimmedLine); } } } catch (IOException e) { System.err.println("读取第三个文件出错: " + e.getMessage()); } } // 抽取公共方法:获取两个文件的公共行 private static Set<String> getCommonLines(String fileAPath, String fileBPath) { Set<String> fileALines = new HashSet<>(); Set<String> commonLines = new HashSet<>(); // 读取第一个文件 try (BufferedReader brA = new BufferedReader(new FileReader(fileAPath))) { String line; while ((line = brA.readLine()) != null) { fileALines.add(line.trim()); } } catch (IOException e) { System.err.println("读取文件" + fileAPath + "出错: " + e.getMessage()); return commonLines; } // 读取第二个文件并比对 try (BufferedReader brB = new BufferedReader(new FileReader(fileBPath))) { String line; while ((line = brB.readLine()) != null) { String trimmedLine = line.trim(); if (fileALines.contains(trimmedLine)) { commonLines.add(trimmedLine); } } } catch (IOException e) { System.err.println("读取文件" + fileBPath + "出错: " + e.getMessage()); } return commonLines; } }
关键说明:
- 把两个文件的公共行查找逻辑抽取成
getCommonLines方法,代码更简洁易维护。 - 先判断前两个文件是否有公共行,如果没有直接结束,避免无效操作。
内容的提问来源于stack exchange,提问作者Glares
相关产品推荐
相关产品推荐

