Related to this question Java writing to a deleted file only in my case i'm doing the reading. And per that comment, yes, Windows block deletes and Unix doesn't. and under unix never throws any IOException
the code is a poor-man's tail -f
, where i have a java thread watching each log file in a directory. my current issue is if the file is deleted, i'm not handling it. I need to abort and start a new thread or something. I didn't even realize it was an issue, as the below code throws no exceptions under Unix
the code
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line = null;
while (true) {
try {
line = br.readLine();
// will return null if no lines added
} catch (IOException e) {
e.printStackTrace();
}
if (line == null) {
// sleep if no new lines added to file
Thread.sleep(1000);
} else {
// line is not null, process line
}
}
Tomorrow I'll try adding this check before the sleep, maybe is sufficient
if (!f.exists()) {
// file gone, aborting this thread
return;
}
Anyone have other ideas?