Ecco un modello per una soluzione che ha opzioni per:
- stdin
- file di
- "heredoc" all'interno il programma (molto probabilmente utile per i test)
.
val input = """ Some string for testing ... """
def main(args: Array[String]) {
val is = if (args.length >= 1) {
if (args(0) == "testdata") {
new StringInputStream(input)
} else {
new FileInputStream(args(0))
}
} else {
System.in
}
import scala.io._
Source.fromInputStream(is).getLines.foreach { line =>
Questo frammento richiede l'utilizzo di uno StringInputStream - e qui è:
class StringInputStream(str : String) extends InputStream {
var ptr = 0
var len = str.length
override def read(): Int = {
if (ptr < len) {
ptr+=1
str.charAt(ptr-1)
} else {
-1
}
}
}
ho creato un modello di supporto standard input/file/heredoc: il codice indicato nella risposta qui sotto. – javadba