The following code opens a file in the UTF-8 encoding system, and translate it to another encoding as the data is read.
File.open("main.rb", "r:utf-8:iso-8859-1") do |f| puts f.external_encoding # ww w . j av a 2 s. c o m first_line = f.gets puts first_line.encoding end