我希望能够将每一行添加到一个字符串。
像这样的格式String =“” depends“ line1 line2 line3 line4 line5 / depends”“

所以在本质上,我想对每一行进行迭代,并从“依赖”到“ /依赖”,包括从头到尾的字符串。我该怎么做呢?

 while(nextLine != "</depends>"){
    completeString = line + currentline;
}


<depends>
line1
line2
line3
line4
line5
line6
</depends

最佳答案

final BufferedReader br = new BufferedReader(new FileReader("path to your file"));
final StringBuilder sb = new StringBuilder();
String nextLine = br.readLine();//skip first <depends>

while(nextLine != null && !nextLine.equals("</depends>"))//not the end of the file and not the closing tag
{
    sb.append(nextLine);
    nextLine = br.readLine();
}

final String completeString = sb.toString();

10-08 03:31