Groovy Console读取输入 [英] Groovy Console read input

查看:149
本文介绍了Groovy Console读取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Groovy,并且正在GroovyConsole中进行试验。

有没有一种方法可以读取用户输入?

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
$ b我已经尝试了下面的代码,但是出现错误。 $ b printInput:

input = br.readLine()

println您输入了:$ input

这是我收到的错误:

抛出的异常
17-Apr-2012 02:52:39 org。 codehaus.groovy.runtime.StackTraceUtils清理

警告:清理stacktrace:

java.io.IOException:流关闭

有什么需要导入的?



任何帮助都会很棒。

谢谢

解决方案

我在这里试图找出从命令行读取用户输入的最简单方法...
我在其他地方找到答案,将在这里发布以记录'real'Groovy方式,因为它仍然缺少:

  def username = System.console()。readLine'你叫什么名字?'
printlnHello $ username

正如Larry Battle所说,如果使用groovy控制台,请务必查看输出的背景黑色窗口并键入输入。

编辑



控制台不可用,例如从您的IDE运行,可能会使用它:

  println你叫什么名字? 
println您的名字是$ {System.in.newReader()。readLine()}


I'm just starting to learn Groovy and I am experimenting in GroovyConsole.

Is there a way I can read user input? I have tried the code below but I get an error.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in))

print "Input:"

input = br.readLine()

println "You entered: $input"

This is the error I am receiving:

Exception thrown
17-Apr-2012 02:52:39 org.codehaus.groovy.runtime.StackTraceUtils sanitize

WARNING: Sanitizing stacktrace:

java.io.IOException: Stream closed

Is there anything I need to import?

Any help would be great.

Thanks

解决方案

I got here trying to find out the easiest way to read user input from the command line... I found the answer elsewhere, will post here to document the 'real' Groovy way as it's still missing:

def username = System.console().readLine 'What is your name?'
println "Hello $username"

As Larry Battle says, if using the groovy console, make sure to look at the background 'black' window for the output and to type input.

EDIT

In an environment where Console is not available, such as running from your IDE, probably, use this instead:

println "What is your name?"
println "Your name is ${System.in.newReader().readLine()}"

这篇关于Groovy Console读取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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