Java BufferedReader-计数包含字符串的行

Java BufferedReader-计数包含字符串的行,java,bufferedreader,Java,Bufferedreader,我使用的是一个.txt文件,其中包含:“Hello world\n您今天做什么?”我想计算一行是否包含字符串,以及行的总数。我使用: File file = new File(file_Path); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; int i=0; int j=0; while ((line = br.readLine()) !

我使用的是一个.txt文件,其中包含:“Hello world\n您今天做什么?”我想计算一行是否包含字符串,以及行的总数。我使用:

File file = new File(file_Path);
try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    int i=0;
    int j=0;
    while ((line = br.readLine()) != null) {
        j++;
        if (line.contains("o")) { //<----------
            i++;
        }
    }
System.out.print("Lines containing the string: " + i + " of total lines " + j-1);
File File=新文件(文件路径);
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦线;
int i=0;
int j=0;
而((line=br.readLine())!=null){
j++;

如果(line.contains(“o”){/我用
StringReader
测试了它

String str = "Hello world\nHow are you doing this day?";
StringReader sr = new StringReader(str);
try {
  BufferedReader br = new BufferedReader(sr);
  String line;
  int i = 0;
  int j = 0;
  while ((line = br.readLine()) != null) {
    j++;
    if (line.contains("world")) { // <----------
      i++;
    }
  }
  System.out
      .println("Lines containing the string: " + i
          + " of total lines " + j);
} catch (Exception e) {
  e.printStackTrace();
}

当其他人回答和评论时,我也认为你可能没有阅读你认为你是的文件…(放松点,这种情况不时发生在每个人身上)

但是,也可能是文件的编码器或您拥有的jdk版本,如果您可以回答:

  • 你用什么来创建这个文件
  • 你正在运行什么操作系统 这个
  • 您使用的是什么JDK
  • 它可以澄清可能发生的事情

    只是想让你知道,我使用jdk8运行了与你相同的代码,对我来说效果很好

    我做的测试如下:

    1)我将您的代码放在一个函数中:

    int countLines(String filename, String wording) {
        File file = new File(filename);
        String line;
        int rowsWithWord = 0;
        int totalRows = 0;
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            while ((line = br.readLine()) != null) {
                totalRows++;
                if (line.contains(wording)) {
                    rowsWithWord++;
                }
            }
        } catch (IOException e) {
            System.out.println("Error Counting: " + e.getMessage());
        }
        System.out.println(String.format("Found %s rows in %s total rows", rowsWithWord, totalRows));
        return rowsWithWord;
    }
    
    2)并运行以下单元测试

    @Test
    public void testFile() {
    
        try (FileWriter fileWriter = new FileWriter(new File("C:\\TEMP\\DELETE\\Hello.txt"));
             BufferedWriter writer = new BufferedWriter(fileWriter)) {
            writer.write("Hello world\nHow are you doing this day?");
        } catch (IOException e) {
            System.out.println("Error writing... " + e);
        }
    
        int countO = fileUtils.countLines("C:\\TEMP\\DELETE\\Hello.txt", "o");
        Assert.assertEquals("It did not find 2 lines with the letters = o", 2, countO);
        int countWorld = fileUtils.countLines("C:\\TEMP\\DELETE\\Hello.txt", "world");
        Assert.assertEquals("It did not find 1 line with the word = world", 1, countWorld);
    
    }
    
    我得到了预期的结果:

    在总共2行中找到2行

    共找到2行中的1行


    你确定你有“世界”而不是“世界”吗?请提供一个简短但完整的程序和文本文件,一起演示问题。尝试打印输出并粘贴到此处。为什么
    j-1
    而不是
    j
    ?为什么命名这些变量i和j而不是
    matchingLineCount
    totalinecount
    ?不,这是“世界”。尝试了不同的单词,但都不起作用。如果我使用j,它会出于某种原因总共打印3行添加
    print
    inside
    while
    循环并检查您是否阅读了正确的文件:)
    @Test
    public void testFile() {
    
        try (FileWriter fileWriter = new FileWriter(new File("C:\\TEMP\\DELETE\\Hello.txt"));
             BufferedWriter writer = new BufferedWriter(fileWriter)) {
            writer.write("Hello world\nHow are you doing this day?");
        } catch (IOException e) {
            System.out.println("Error writing... " + e);
        }
    
        int countO = fileUtils.countLines("C:\\TEMP\\DELETE\\Hello.txt", "o");
        Assert.assertEquals("It did not find 2 lines with the letters = o", 2, countO);
        int countWorld = fileUtils.countLines("C:\\TEMP\\DELETE\\Hello.txt", "world");
        Assert.assertEquals("It did not find 1 line with the word = world", 1, countWorld);
    
    }