groovy script Error

2019-08-12 02:47发布

问题:

I am newbie to groovy. In command prompt (I am not using any IDE), I typed following : (No Problem with environment variables settings please)

groovy -n -e "println line.toLong()" data.txt

Error :

Caught: java.io.IOException: Invalid argument
java.io.IOException: Invalid argument

data.txt is there in that directory (verified using TAB key)


Not sure why its throwing Error ????

[data.txt contains raw data in following format]

1

2

3

4

回答1:

I tried to duplicate the problem with Groovy 2.3.6 and Java 1.7.0_60 on Linux and had no issue:

$ echo -e "1\n2\n\3\n\4" > data.txt
$ groovy -n -e "println line.toLong()" data.txt
1
2
3
4

With a non-existing file:

$ groovy -n -e "println line.toLong()" bogus.txt
Caught: java.io.FileNotFoundException: bogus.txt
java.io.FileNotFoundException: bogus.txt

With non-numeric data:

$ echo -e "a\nb\nc\nd" > data.txt
$ groovy -n -e "println line.toLong()" data.txt
Caught: java.lang.NumberFormatException: For input string: "a"
java.lang.NumberFormatException: For input string: "a"
        at script_from_command_line.run(script_from_command_line:1)

And it even worked with CR/LF EOL:

$ echo -e "1\r\n2\r\n3\r\n4\r" > data.txt
$ groovy -n -e "println line.toLong()" data.txt
1
2
3
4