require 'java'It may not be the perfect solution (I'd like to see a regular Ruby solution) but I suppose leveraging the Java libraries was the whole point of JRuby in the first place wasn't it!
import java.io.InputStreamReader
import java.io.ByteArrayInputStream
import java.io.BufferedReader
data = ''
File.open("text.txt", 'rb'){|f| data = f.read}
#strip the BOM (Byte Order Marker)
data.slice!(0..1)
#let Java deal with the UTF-16 encoding
reader = BufferedReader.new(
InputStreamReader.new(
ByteArrayInputStream.new(data.to_java_bytes), 'UTF-16LE'))
while ((s = reader.read_line) != nil)
puts s
end
(Executing File.open("text.txt", 'rb'){|f| puts f.read} looked fine on my Mac, except for the BOM, but looked terrible in the console on Windows. The solution above actually converts the text from double byte to single byte characters)
No comments:
Post a Comment