【问题标题】:How can i read file name of utf8 encodng in euc-kr encoding system using java?如何使用 java 在 euc-kr 编码系统中读取 utf8 编码的文件名?
【发布时间】:2019-04-19 07:17:46
【问题描述】:

我使用 euc-kr 编码系统。 我的程序由 Java 制作。 它读取文件名。 但程序无法读取某些文件名。 (有些文件名是utf8编码)

所以我尝试了这个

File dir = new File(dirPath);
File[] fileList = dir.listFiles(); //get files that file name is utf8 encoding
String cangedEncodingstr= new String(fileList[0].getName().getBytes("euc-kr"), "euc-kr"); // This is still an invalid string

.

我认为这个字符串在调用dir.listFiles()时已经损坏了

File dir = new File(dirPath);
File[] fileList = dir.listFiles(); //get files that file name is utf8 encoding

fileList[0].getName(); // broken String
fileList[0].isFile(); //false
fileList[0].isDirectory(); //false

utf8 编码的字符串也期望isFile () 为真。

提前感谢您的回复。 :-)

【问题讨论】:

    标签: java encoding utf-8


    【解决方案1】:

    File.getName() 返回一个String。因此,您无需在 Java 中做任何其他事情。

    Java 中的String 是纯文本数据类型。它包含 UTF-16 代码单元的计数序列,即char 的计数序列。每个字符串函数都是基于此编写的。您通常不需要考虑这一点,因为大部分文本处理都不依赖它。少数情况是当您对不是从 indexOf 或类似文本函数获得的索引进行计数或拆分时。

    许多语言都是这样的,例如.NET 语言、VB4/5/6/A/Script、JavaScript、SQL NVARCHAR 或 NCHAR。其他人的“字符串”实际上只是字节字符串,它可能是具有多种编码之一的文本,例如 Lua、C、C++ ……。其他的有不同类型的字符串来保持字节字符串和字符串之间的差异,例如 Python。其他的字符串包含一个属性来指示字符串的字符编码,例如R。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2018-03-27
      • 2015-11-30
      • 1970-01-01
      相关资源
      最近更新 更多