I was practicing using byte streams in Java when I came across this code:
import java.io.*;
public class CopyBytes_practice {
public static void main(String args[]) throws IOException {
FileInputStream f=null;
FileOutputStream fo=null;
int c;
int d;
try {
f=new FileInputStream("a.png");
fo=new FileOutputStream("b.png");
c=f.read();
while(c != -1){
fo.write(c);
}
} finally {
if (f != null) {
f.close();
}
if (fo != null) {
fo.close();
}
}
}
I used a 35kb a.png file and a 0kb b.png file to execute the code, but the code ran forever - the size of b.png reached 905mb before I stopped the JVM manually.
I am confused, why is the end of file status not returned? Is it not supported in Binary files, or is something else wrong?