Я пытаюсь написать строку в конце текстового документа, но ничего не печатается в конце hello2.txt
с этим кодом, который печатается только hello.txt
. Эта строка last line in the text document.
не печатается в конце hello2.txt
. Как я могу это исправить?Запись строки в конце текстового документа
Я ценю любую помощь.
hello.txt
I am at home
how are you brother?
I am facing problem
hello2.txt должно выглядеть как этот
I am at home
how are you brother?
I am facing problem
last line in the text document.
Код:
public static void main(String[] args) {
File file = new File("D:\\hl_sv\\hello.txt");
try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");
Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.isEmpty()) {
System.out.println("last line in the text document");
writer.println("last line in the text document.");
} else {
writer.println(line);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Я не думаю, что вы хотите использовать isEmpty(), чтобы проверить последнюю строку. –
@XorProgramming - это не тот же вопрос, и не та же проблема. – MByD
дубликат этого вопроса http://stackoverflow.com/questions/30714916/check-whether-the-line-is-the-last-one-in-the-text-document/30715315#30715315 –