将cp-1252编码为utf-8? [英] Encoding cp-1252 as utf-8?
问题描述
我想写一个将在linux服务器上运行的Java应用程序,但它将使用cp-1252作为字符集来处理在传统Windows机器上生成的文件。有没有反编译这些文件为utf-8而不是cp-1252它生成为?
I am trying to write a Java app that will run on a linux server but that will process files generated on legacy Windows machines using cp-1252 as the character set. Is there anyway to encode these files as utf-8 instead of the cp-1252 it is generated as?
推荐答案
名称以及内容是一个问题,解决问题的最简单的方法是将Linux机器上的 locale
设置为基于 ISO-8859 -1
,而不是 UTF-8
。您可以使用 locale -a
列出可用的语言环境。例如,如果您有 en_US.iso88591
,您可以使用:
If the file names as well as content is a problem, the easiest way to solve the problem is setting the locale
on the Linux machine to something based on ISO-8859-1
rather than UTF-8
. You can use locale -a
to list available locales. For example if you have en_US.iso88591
you could use:
export LANG=en_US.iso88591
这样Java将使用ISO-8859-1作为文件名,大概够好了。要运行Java程序,您还必须设置 file.encoding
系统属性:
This way Java will use ISO-8859-1 for file names, which is probably good enough. To run the Java program you still have to set the file.encoding
system property:
java -Dfile.encoding=cp1252 -cp foo.jar:bar.jar blablabla
如果没有ISO-8859-1语言环境可用,您可以使用 localedef
生成一个语言环境。安装它需要根访问。实际上,您可以生成使用CP-1252的区域设置(如果它在系统上可用)。例如:
If no ISO-8859-1 locale is available you can generate one with localedef
. Installing it requires root access though. In fact, you could generate a locale that uses CP-1252, if it is available on your system. For example:
sudo localedef -f CP1252 -i en_US en_US.cp1252
export LANG=en_US.cp1252
这样,对于所有I / O,Java应该默认使用CP1252,包括文件名。
This way Java should use CP1252 by default for all I/O, including file names.
进一步展开: http://jonisalonen.com/2012/java-and-file-names-with-invalid-characters/
这篇关于将cp-1252编码为utf-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!