2012-11-22 46 views
0

下面的程序适用于ruby,但在到达带有特殊字符(如我用于测试的文件,名为“mão.txt”)的文件时,给我带来了JRuby问题:使用JRuby无法打开包含特殊字符的文件

# coding: utf-8 

puts "(A) #{__ENCODING__}" 

puts "(B)" + "".encoding.to_s 
puts "(C)" + String.new.encoding.to_s 

Dir.glob("./fixtures/*").each do |f| 
    puts "(D)" + f.encoding.to_s + " " + f 
    File.open(f) 
    g = File.expand_path(f) 
    puts "(E)" + g + " " + g.encoding.to_s 
    File.open(g) 
end 

JRuby的结果是:

(A) UTF-8 
(B)UTF-8 
(C)ASCII-8BIT 
(D)ASCII-8BIT ./fixtures/mão.txt~ 
Errno::ENOENT: No such file or directory - ./fixtures/mão.txt~ 
    initialize at org/jruby/RubyFile.java:315 
     open at org/jruby/RubyIO.java:1176 
     (root) at encoding.rb:10 
     each at org/jruby/RubyArray.java:1612 
     (root) at encoding.rb:8 

我使用Ubuntu 12.10的JRuby 1.7.0和Java 1.7.0_09

我打算有战争打包的应用程序ble,所以我担心命令行参数不是一个选项。

回答

1

这是一个带有Dir.glob的报告。

+0

我已经提交[补丁](https://github.com/jruby/jruby/pull/407)这个问题是现在已经合并成核心;所以现在问题应该得到解决。 –

1

正如Sebastien所说,这是一个已知的错误。

我实际上找到了这个bug的解决方法。而不是使用Dir.glob,在这种情况下,我需要目录中的每个文件,我可以简单地使用Dir.entries,它工作正常。

程序可以更改为:

# coding: utf-8 
path = File.expand_path(File.dirname(__FILE__)) 
puts "(A) #{__ENCODING__}" 

puts "(B)" + "".encoding.to_s 
puts "(C)" + String.new.encoding.to_s 

dir = "#{path}/fixtures/" 
entries = Dir.entries(dir) - ['.', '..'] 
entries.each do |f| 
    puts "(D)" + f.encoding.to_s + " " + f 
    file = "#{dir}/#{f}" 
    puts "(E)" + file.encoding.to_s + " " + file 
    #f.encode("UTF-8") 
    File.open(file) 
    g = File.expand_path(file) 
    puts "(F)" + g + " " + g.encoding.to_s 
    File.open(g) 
end