Forum Discussion

ABCDEFG's avatar
ABCDEFG
Contributor
14 years ago

Groovy: Read File Line by Line

Hi,

Can some one help me to read a file line by line,

I have this code - but this print all the content. I need line by line print, hence i can access any line dynamically.

//read from file

myFileDirectory = "C:\\Documents and Settings\\ABCEDFG\\Desktop\\soapUI\\"
myFileName = "params.txt"
myFile = new File(myFileDirectory + myFileName)

printFileLine = { log.info "File line: " + it }
myFile.eachLine(0, printFileLine)


Please help -

^Thanks

6 Replies

  • Maybe


    printFileLine = { log.info "File line: " + it + "\n"}


    ?

    Haven't tested this myself.

    I'm betting there's an answer on Google somewhere if that doesn't work.

    Also try reading the book "Groovy in Action" by Konig et al.
  • This is not working... I need to read line by line. if for ex: i need to print only 5th line of a text file. how can i?

    Is there any website giving free download of this book?

    ^Regards
  • deepesh_jain's avatar
    deepesh_jain
    Frequent Contributor
    Hi ABCDEFG,

    You should be able to use java libraries to read line by line. Something like this:

    FileInputStream fstream = new FileInputStream("textfile.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    def strLine;

    while ((strLine = br.readLine()) != null)
    {
    log.info "$strLine";
    }

    in.close();


    Let me know if this works.

    Regards,
    Deepesh Jain
  • No Deepesh, its giving following error message in soapUI.




    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script8.groovy: 3: expecting EOF, found 'in' @ line 3, column 17. DataInputStream in = new DataInputStream(fstream); ^ org.codehaus.groovy.syntax.SyntaxException: expecting EOF, found 'in' @ line 3, column 17. at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:107) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:163) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:839) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:544) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:520) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:497) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:731) at groovy.lang.GroovyShell.parse(GroovyShell.java:743) at groovy.lang.GroovyShell.parse(GroovyShell.java:770) at groovy.lang.GroovyShell.parse(GroovyShell.java:761) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:148) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:148) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:274) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: Script8.groovy:3:17: expecting EOF, found 'in' at groovyjarjarantlr.Parser.match(Parser.java:211) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.compilationUnit(GroovyRecognizer.java:780) at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:130) ... 20 more 1 error


    Pls help -

    ^Thanks
  • deepesh_jain's avatar
    deepesh_jain
    Frequent Contributor
    Can you try renaming the "in" variable in your script to something else, in i beleive is a keywork in groovy that why the exception.