[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-user
Subject: Re: [groovy-user] How to easily read from System.in?
From: Vaclav Pech <vaclav.pech () seznam ! cz>
Date: 2009-06-21 6:52:20
Message-ID: 4A3DD8A4.9010702 () seznam ! cz
[Download RAW message or body]
Hello Michael,
you'll have to keep the reader open until you're done processing the input.
System.in.withReader {
print 'input'
println it.readLine()
...
//do something else
...
print 'input'
println it.readList()
}
Leaving the withReader block closes the stream.
Regards,
Vaclav
MichaelM wrote:
> If I try this idiom in more than one place in my script, it results in
> java.io.IOException: Stream closed.
> Is this a bug, or the expected bahavior? If expected, what is the
> recommended way to get user input at more than one place in the script?
>
> System.in.withReader {
> print 'input: '
> println it.readLine()
> }
>
> // Repeat further down in the script for other input:
>
> System.in.withReader {
> print 'input: '
> println it.readLine()
> }
>
> Caught: java.io.IOException: Stream closed
> at test$_run_closure2.doCall(test.groovy:10)
> at test.run(test.groovy:8)
>
>
> Dierk König wrote:
>
>> a bit more Groovy idiomatic:
>>
>> System.in.withReader {
>> print 'input: '
>> println it.readLine()
>> }
>>
>> cheers
>> Dierk
>>
>> Am 27.11.2008 um 21:27 schrieb Claus Hausberger:
>>
>>
>>> Hello
>>>
>>> I want to port some old Perl scripts to Groovy. In those scripts I
>>> have to read a lot from System.in.
>>>
>>> I got this program working:
>>>
>>>
>>> BufferedReader br = new BufferedReader(new
>>> InputStreamReader(System.in))
>>> print "input:"
>>> String input = br.readLine()
>>> println "input was:$input:"
>>>
>>>
>>> It does exactly what I want but I wanted to ask if there is a
>>> shorter, more Groovy way to do this.
>>>
>>>
>>> Claus
>>> --
>>> Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit
>>> allen: http://www.gmx.net/de/go/multimessenger
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>> http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>> http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>>
>
>
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic