Appending to a UNICODE encoded file
Dear Fellow Developers,
I ran into an issue trying to append to a UNICODE encoded file. Below is a test program for you to try.
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
publicclass UnicodeFileWriter{
publicstaticvoid main(String[] args)throws IOException{
BufferedWriter writer =new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("unicode.txt",true),"unicode"));
writer.write("Hello World!");
writer.close();
}
}
When I run the above code the first time, it works fine and the file contents are as shown below:
Hello World!
When I run it the second time, the file contents are as shown below:
Hello World!?Hello World!
Notice the invalid character (?) between the first and the second write operation. Looking at the file with a HEX viewer, I see the characters (0xFF and 0xFE) after the first write (or before the second write) operation. The same characters appear at the very beginning of the file.
Any ideas on why we are getting these in append mode? Are they supposed to be present there? If not, is there a way to remove them?
Thanks in advance for your help.

