在intellij中运行程序时模拟stdin的输入 [英] Simulate input from stdin when running a program in intellij

查看:694
本文介绍了在intellij中运行程序时模拟stdin的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将命令行参数配置为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

  • file
  • $ b程序中$ b
  • heredoc(最有可能用于测试)

  • stdin
  • file
  • "heredoc" within the program (most likely useful for testing)

  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中运行程序时模拟stdin的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆