在intellij中运行Scala程序时模拟来自stdin的输入 [英] Simulate input from stdin when running a scala program in intellij
本文介绍了在intellij中运行Scala程序时模拟来自stdin的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以将命令行args配置为intellij以便进行stdin重定向?
Is there any way to configure the command line args to intellij for stdin redirection?
类似的东西:
运行|编辑运行配置|脚本参数
Run | Edit Run Configurations | Script Parameters
/shared/java/paf-rules.properties 2 < /shared/java/testdata.csv
推荐答案
以下是解决方案的模板,其中包含以下选项:
Here is a template for a solution that has options for:
- stdin
- 文件 程序中的
- "heredoc"(最有可能用于测试)
.
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 =>
此代码段需要使用StringInputStream-在这里是
This snippet requires use of a StringInputStream - and here it is:
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
}
}
}
这篇关于在intellij中运行Scala程序时模拟来自stdin的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文