Here is what is happening ......
1. you are taking "hello" and writing it into the file. So file now contains "hello"
2. you are taking "world" and replacing all content in file with "world" So file now contains "world"
3. you are taking "everyone" and replacing all content in file with "everyone" So file now contains "everyone"
So, when you open the file, you have just one
word "everyone" (which is clearly not expected)
what should have been done:
approach 1: StringBuffer
append all words into a
String Buffer and then write this String Buffer into the file just once
approach 2: File Append
Open a file for writing with the mode as append instead of overwrite and then write every single word into it.
Hope it helps you mate.