Skip to content

Instantly share code, notes, and snippets.

@codingtony
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingtony/c21e278b3111d5256f0d to your computer and use it in GitHub Desktop.
Save codingtony/c21e278b3111d5256f0d to your computer and use it in GitHub Desktop.
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
public class ReadPeriodically {
static public void main(String... args) throws Exception {
final InputStream is = new FileInputStream("/tmp/logfile.txt");
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
if (is.available() > 0) {
StringWriter sw = new StringWriter();
IOUtils.copy(is, sw,"UTF-8");
System.out.print(sw.toString());
}
} catch (IOException e) {
System.out.println("Problem reading the file");
}
}
}, 0, 15, TimeUnit.SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment