Last active
August 29, 2015 14:02
-
-
Save codingtony/c21e278b3111d5256f0d to your computer and use it in GitHub Desktop.
Read Periodically a file ( http://stackoverflow.com/questions/24088659/reading-inputstream-periodically/24088727 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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