I am saving .txt file as a binary format.But at the last line of a binary file,one more character is saved i.e ÿ
Why this has happened ?
My code is like this :
FileOutputStream fout = null;
InputStream fin;
String path="something";
int i = 0;
fout = new FileOutputStream(path);
do {
i=fin.read();
fout.write(i);
} while(i != -1);
fout.flush();
fout.close();
It’s pretty obvious: The ÿ character (unicode U+00FF) you are seeing is the -1 you are writing just before quitting the loop. You should not use
do ... while()here, instead use the more common idiom: