My interest of piqued I wrote this little test:
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("/home/nick/foo");
FileOutputStream fileOutputStream = new FileOutputStream("/home/nick/bar");
fileOutputStream.write(fileInputStream.read());
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();
}
It worked as expected - read a single byte from /home/nick/foo
and wrote it to /home/nick/bar
EDIT:
Updated program:
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("/home/nick/foo");
FileOutputStream fileOutputStream = new FileOutputStream("/home/nick/bar");
while (fileInputStream.available()>0) {
fileOutputStream.write(fileInputStream.read());
}
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();
}
Copied the entire file. (note - I would not recommend copying a file a byte at a time, use the Buffered I/O classes to copy whole chunks)
Did you by any chance forget to flush()
and close()
to OutputStream?
fileInputStream.available()
for a loop read. It doesn't do what you think it does. It just tells you how many bytes are available without blocking - which is not necessarily the length of the file. This could affect your results regaldless of the variable issue. – Roadability