Perl中读取含有中文的路径 或 含有中文的文件名

来源:互联网 发布:卓易云软件下载 编辑:程序博客网 时间:2024/05/18 03:45
File name is encoded in UTF-16LE on Windows. Windows offers two interfaces to access the file by name. The first one is the regular char inteface, which requires file name represented in a native encoding such as cp936 for Simplified Chinese. The second one is the wide char interface, which allows file name expressed in UTF-16.  Perl's built-in open() function doesn't use the wide char interface, so the non-English file name must be encoded in the local encoding before passing to open(). The Encode::Locale helps Perl programmer to determine the native encoding to use, here is an example usage:
 
Perl代码  
  1. #!/usr/bin/perl -w   
  2.   
  3. use strict;   
  4. use utf8;   
  5. use Encode::Locale qw($ENCODING_LOCALE_FS);   
  6. use Encode;   
  7.   
  8. printf "%s\n", $ENCODING_LOCALE_FS;   
  9. my $f1 = encode(locale_fs => '数据库日志.lg');   
  10. open(my $fh, "<", $f1) or die "Can't open file $f1 due to: $!";   
  11. while(<$fh>) {   
  12.     print;   
  13. }   
  14. close $fh;  

 

 Python seems behave similar to Perl. Java is capable of taking file name represented in both unicode and native encoding.

引入第5行,根据要求编写第9行,可在MS-DOS下输出含有中文的路径或文件名
0 0
原创粉丝点击