I'm studying java in a certain website and I found this
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
What is the meaning of c = in.read()) != -1
?Why can it be an integer?
Your FileInputStream
is a stream of bytes.
When you call FileInputStream.read()
it return a byte
from the stream or return -1 if reach end of stream.
When you write: c = in.read()) != -1
,
byte
is read from stream, widening as int
, assign back to c
and -1
;