Here is an example which shows that it works just fine.
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
File file = File.createTempFile("deleteme", ".txt");
file.deleteOnExit();
Thread main = Thread.currentThread();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
do {
Thread.sleep(200);
long length = file.length();
System.out.println("File " + file + " is " + length + " bytes long.");
} while(main.isAlive());
System.out.println("Finished");
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
}, "monitor");
t.start();
FileOutputStream fos = new FileOutputStream(file);
for(int i=0;i<2000;i++) {
fos.write("words words words words words words words words words words words\n".getBytes());
Thread.sleep(1);
}
fos.close();
}
}
prints something like.
File /tmp/deleteme4214599935706768614.txt is 11880 bytes long.
File /tmp/deleteme4214599935706768614.txt is 23562 bytes long.
File /tmp/deleteme4214599935706768614.txt is 35376 bytes long.
File /tmp/deleteme4214599935706768614.txt is 47256 bytes long.
File /tmp/deleteme4214599935706768614.txt is 59136 bytes long.
File /tmp/deleteme4214599935706768614.txt is 70950 bytes long.
File /tmp/deleteme4214599935706768614.txt is 82830 bytes long.
File /tmp/deleteme4214599935706768614.txt is 94644 bytes long.
File /tmp/deleteme4214599935706768614.txt is 106524 bytes long.
File /tmp/deleteme4214599935706768614.txt is 118338 bytes long.
File /tmp/deleteme4214599935706768614.txt is 130218 bytes long.
File /tmp/deleteme4214599935706768614.txt is 132000 bytes long.
Finished