Ask Your Question
0

How do I read a whole file line-by-line in a Jython processor

asked 2018-01-20 11:43:39 -0500

metadaddy gravatar image

I want to read whole file content in a Jython processor. I've tried this, but it's not working:

input_stream = record.value['fileRef'].getInputStream()
[x for x in input_stream.read()]
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-01-20 11:44:27 -0500

metadaddy gravatar image

getInputStream() returns a Java InputStream object, so you can read it line-by-line like this:

from java.io import InputStreamReader
from java.io import BufferedReader

for record in records:
  try:
    reader = BufferedReader(InputStreamReader(record.value['fileRef'].getInputStream()))
    try:
      while True:
        line = reader.readLine()
        if line is None:
          break
        log.debug("LINE: {}", line)

    finally:  
      reader.close()

    output.write(record)

  except Exception as e:
    error.write(record, str(e))
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-01-20 11:43:39 -0500

Seen: 161 times

Last updated: Jan 20