Я пытаюсь прочитать текст из файла, выполнить некоторую операцию и вывести результат в другой файл, но по какой-то причине он печатает только одну строку результатов.
Scanner file = new Scanner(new FileInputStream("/.../file.txt"));
PrintWriter p = new PrintWriter(new FileWriter("/.../output.txt"));
int count = 0;
while (file.hasNextLine())
{
String s = file.nextLine();
count++;
try
{
if(s.contains("#AVFC")){
p.printf("There are %d words on this line ", s.split("\\s").length-1);
p.println(count);
p.close();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
file.close();
это вывод;
There are 4 words on this line 1
но вывод должен быть:
There are 4 words on this line 1
There are 10 words on this line 13
There are 8 words on this line 16