Java:扫描仪未扫描完整的行

Java:扫描仪未扫描完整的行,java,java.util.scanner,Java,Java.util.scanner,我有以下文本文件(不是java文件) 我正在尝试使用以下代码读取该文件 String editedSection = null; boolean containSection = false; Scanner in = new Scanner(new FileReader(directoryToAddFile)); while(in.hasNextLine()) { if(in.nextLine().contains("/*START OF CHANGES TO CODE*/")) {

我有以下文本文件(不是java文件)

我正在尝试使用以下代码读取该文件

String editedSection = null;
boolean containSection = false;
Scanner in = new Scanner(new FileReader(directoryToAddFile));
while(in.hasNextLine()) {
    if(in.nextLine().contains("/*START OF CHANGES TO CODE*/")) {
        containSection = true;
        editedSection = in.nextLine().toString();
    } else if (containSection == true) {
        editedSection = editedSection+in.nextLine().toString();
    } else if (in.nextLine().contains("/*END OF CHANGES TO CODE*/")) {
        containSection = false;
        editedSection = in.nextLine().toString();
    }
    in.nextLine();
}

所以基本上我想让它读取一个文件,直到它看到
/*开始对code*/
的更改,然后开始将这之后的每一行添加到一个字符串中,直到它到达
/*结束对code*/
的OD更改。但当它在读台词时,它会忽略一些台词和其他台词的一部分。有人知道怎么做吗?

你在.nextLine()中调用了
很多次,在
循环中。对我来说,这听起来真是个坏主意。它将在每次迭代中执行多少次将取决于它进入的位。。。讨厌

我建议你用

while(in.hasNextLine()) {
    String line = in.nextLine();
    // Now use line for the whole of the loop body
}

这样,您就不会因为只是为了检查而读取行而意外跳过行。

您在.nextLine()中调用了
循环中调用了很多次。对我来说,这听起来真是个坏主意。它将在每次迭代中执行多少次将取决于它进入的位。。。讨厌

我建议你用

while(in.hasNextLine()) {
    String line = in.nextLine();
    // Now use line for the whole of the loop body
}

这样,你就不会因为只是为了检查而阅读而意外地跳过行。

这正是问题所在,我甚至没有想到在我叫它时它会移到下一行。。。干杯:)这正是问题所在,我甚至没有想到在我叫它的时候它会转到下一行。。。干杯:)