I am trying to write line at the end of my text document but nothing is being printed at the end of the hello2.txt
with this code just the hello.txt
is being printed. This line last line in the text document.
is not being printed at the end of the hello2.txt
. How can I fix that?
I appreciate any help.
hello.txt
I am at home
how are you brother?
I am facing problem
hello2.txt Should looks like this
I am at home
how are you brother?
I am facing problem
last line in the text document.
Code:
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();
}
}