Would you please tell me why does this character ” ÿ ” appears at the end of my output file. (I use try/catch)
File f1 = new File("C:/Users/NetBeansProjects/QuestionOne/input.txt");
File f2 = new File("C:/Users/NetBeansProjects/QuestionOne/output.txt");
fin = new FileInputStream(f1);
fout = new FileOutputStream(f2);
do {
i = fin.read();
fout.write(i);
} while (i != -1);
The code copies the file content but it ends it with this strange character.
Do I have to include the entire code?
Thanks.
The method
fin.read()returns -1 when there’s nothing left to read; but you’re actually writing that -1 tofout, even though it didn’t occur infin. It shows up as that ÿ character.One way to write your loop to avoid this problem is