Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

(Jython) How to read the contents of a file into a String

LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in SOAtest
using Jython
To read the contents of a file into a String using a Jython script you can use the following method:

CODE
from java.lang import *
from java.io import *
from soaptest.api import *

def getContents(input, context):
  contents = StringBuffer()
  reader = BufferedReader(FileReader(File("c:\Documents and Settings\jhendrick\Desktop\test.txt")))
  line = String()
  while line != None:
    line = reader.readLine()
    if line != None:
      contents.append(line)
    contents.append(System.getProperty("line.separator"))
  reader.close()
  return contents.toString()
Tagged:

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    If you have trouble with the above script, then you will also want to visit the following forum post:

    Python script fails when using JRE 1.5.0

    There, you will find a download and instructions. Please try this first.
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Is it possible to use relative path, instead of absolute ?
    I am using SOA 6.1, and it doesnt work for me.
    reader = BufferedReader(FileReader(File("test.txt")))
    Lech

      reader = BufferedReader(FileReader(File("c:\Documents and Settings\jhendrick\Desktop\test.txt")))
      line = String()
      while line != None:
        line = reader.readLine()
        if line != None:
          contents.append(line)
        contents.append(System.getProperty("line.separator"))
      reader.close()
      return contents.toString()[/code]

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    To get the absolute path of a file. Assuming that the file is in the same project folder. Following code can be used.

    CODE
    filePath = str(context.getAbsolutePathFile("test.txt"))
    reader = BufferedReader(FileReader(File(filePath)))

    Is it possible to use relative path, instead of absolute ?
    I am using SOA 6.1, and it doesnt work for me.
    reader = BufferedReader(FileReader(File("test.txt")))
    Lech

      reader = BufferedReader(FileReader(File("c:\Documents and Settings\jhendrick\Desktop\test.txt")))
      line = String()
      while line != None:
        line = reader.readLine()
        if line != None:
          contents.append(line)
        contents.append(System.getProperty("line.separator"))
      reader.close()
      return contents.toString()[/code]

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    SOAtest has since added utility methods so that you don't need to read a file line-by-line.

    For an Extension Tool when you know the path to a file:

    CODE
    from com.parasoft.api import IOUtil
    from java.io import File

    def main(input, context):
        # Create java.io.File however you can.
        file = File("path/to/what/you/want")
        return IOUtil.readTextFile(file)

    For a standalone Extension Tool when you have configured the Input tab to use a file as the input:

    CODEfrom com.parasoft.api import IOUtil

    def main(input, context):
        # input is of type java.io.File
        return IOUtil.readTextFile(input)

    IOUtil.readTextFile returns a string. If you have a binary file, you can use IOUtil.readBinaryFile, which returns a byte array (byte[] in Java).