Java - 用readLine读取二进制文件 [英] Java - reading file as binary with readLine
问题描述
我有一个逐行读取文件的Ruby代码,并检查是否需要读取下一行到某个块,或者它应该处理该块,并继续阅读文件解析每一行。
这里是:
File.open(ARGV [0],'rb')do | F |
fl = false
text =''
f.readlines.each do | line |
if(line =〜/ ^ end_block /)
fl = false
#解析文本变量
结束
文本+ =行如果fl == true
if(line =〜/ ^ start_block /)
fl = true
end
end
end
我需要打开文件作为二进制阅读,仍然需要一个 readLine
方法。
所以,问题是:如何与Groovy / Java完全相同?
java.io.DataInputStream
,它提供了一个<$ c readLine()方法和 readFully(byte [])
和 read(byte [])
readLine 的JavaDoc说:不推荐使用,编码可能不合适(在JavaDoc中阅读详细信息)。 因此,请仔细考虑一下您的真实需求,如果这是您的情况的适当折衷。
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.DataInputStream
which provides both a readLine()
method and readFully(byte[])
and read(byte[])
methods.
Warning: The JavaDoc for readLine
says, 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.
这篇关于Java - 用readLine读取二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!