I have a Ruby code that reads file line-by-line and checks if it needs to read the next line to some block or it should handle that block and continue reading file parsing each line.
Here’s it:
File.open(ARGV[0], 'rb') do |f|
fl = false
text = ''
f.readlines.each do |line|
if (line =~ /^end_block/)
fl = false
# parse text variable
end
text += line if fl == true
if (line =~ /^start_block/)
fl = true
end
end
end
E.g. i need the file to be opened for reading as binary and still i need a readLine method.
So, the question is: how can i do exactly the same with Groovy/Java?
You can use
java.io.DataInputStreamwhich provides both areadLine()method andreadFully(byte[])andread(byte[])methods.Warning: The JavaDoc for
readLinesays, it is deprecated and that the encoding might be inappropriate (read details in JavaDoc).So think twice about your real requirements and if this is a suitable trade-off in your case.