Skip to content

Instantly share code, notes, and snippets.

@awwsmm
Last active November 9, 2017 16:44
Show Gist options
  • Save awwsmm/361142ec2991967a348e2db9f541a69a to your computer and use it in GitHub Desktop.
Save awwsmm/361142ec2991967a348e2db9f541a69a to your computer and use it in GitHub Desktop.
Read a file while it's being written (good for analysis of streaming data)
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStreamReader
import java.lang.Thread
import java.nio.charset.StandardCharsets
import scala.io.StdIn
import scala.Console
object LiveReader {
var userInput = ""
var killedByUser = false
var running = true
val filepath = "path/to/fileToBeRead.txt"
val file = new FileInputStream(filepath)
val buffer = new BufferedInputStream(file, 4096)
val lines = new BufferedReader(new InputStreamReader(buffer, StandardCharsets.UTF_8))
var lastLine = ""
def main(args: Array[String]) = {
val countThread = new Thread {
override def run {
while (!killedByUser && running) {
try {
// to read char-by-char, use the BufferedInputStream by itself:
// if (buffer.available() > 0) {
// print(buffer.read().toChar)
if(lines.ready()) {
lastLine = lines.readLine()
println(lastLine)
} else {
try {
Thread.sleep(500) // check file every 500 ms
} catch {
case ex: InterruptedException => {
println("RealTime: file read interrupted!")
running = false
} } }
} catch {
case ex: IOException => {
println("RealTime: file read interrupted!")
ex.printStackTrace()
} } } } }
countThread.start()
println("Reading file...")
/// run until the user hits the kill switch
while(!killedByUser) {
// listen for user input here [ http://bit.ly/2yd5Dl8 ]
while (!Console.in.ready) {
Thread.sleep(500) // check user input every 500 ms
}
// process user input here
if(!killedByUser) {
val userInput = StdIn.readLine()
// echo user input
print(f" You entered: '$userInput': ")
/// define user commands here
if (List("q","Q","quit","Quit","QUIT","exit","EXIT","Exit","x","X") contains userInput) {
print(f" program terminated.\n\n")
killedByUser = true
} else if (List("l","L","line","Line","LINE") contains userInput) {
print(f" last line read was:\n ${lastLine}\n\n")
} else { // reset the Console if user enters nonsense
print(f" undefined command\n\n")
Console.in.mark(0)
Console.in.reset()
}
// do other stuff
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment