[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:       MichaelM <t3nq9nf02 () sneakemail ! com>
Date:       2009-06-22 18:28:45
Message-ID: 24152725.post () talk ! nabble ! com
[Download RAW message or body]


Vaclav,

I realize that leaving the withReader block closes the stream. But why isn't
it re-opened with the next withReader usage?

This is inconsistent with using a file. Eg the code below works just fine:

example = new File('test.txt')
example.withReader {
    print 'input'
    println it.readLine()
}
example.withReader {
    print 'input'
    println it.readLine()
}

So my question was: is this inconsistency there by design? Or is it a bug.


Thanks,
M.


Vaclav Pech wrote:
> 
> 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
> 
> 
> 
> 

-- 
View this message in context: \
http://www.nabble.com/How-to-easily-read-from-System.in--tp20725175p24152725.html \
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
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