本文介绍了在多个操作系统中读取文件名,而没有Ruby的编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得UTF-8中的所有文件名。例如,在Windows中读取文件名后,我执行

I want to get all filenames in UTF-8. For example, after I read filename in Windows, I do

filename = Iconv.iconv("UTF-8", "Windows-1251", filename)



。可能存在一些方法来确定操作系统文件名编码?

In Ubuntu I don't convert filename and get it in UTF-8. Maybe exists some method to determine OS filename encoding?

推荐答案

我在Ruby 1.9上做这个, UTF-8:

I do this on Ruby 1.9 when I want to make sure things are in UTF-8:

if filename.encoding.to_s != 'UTF-8'
  filename.encode!('UTF-8')
end

除非您的操作系统提供的文件名编码系统不支持在文件名上找到的一些特殊字符,它可以编码为UTF-8没有hickup。

Unless your OS gives a file name encoded with a coding system which doesn't support some special characters found on the file name, it can be encoded to UTF-8 without a hickup.

这篇关于在多个操作系统中读取文件名,而没有Ruby的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:28