Linux 将 cp-1252 编码为 utf-8?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12045581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:07:07  来源:igfitidea点击:

Encoding cp-1252 as utf-8?

javalinuxutf-8character-encodingcp1252

提问by IAmYourFaja

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?

我正在尝试编写一个 Java 应用程序,该应用程序将在 linux 服务器上运行,但它将使用 cp-1252 作为字符集处理在旧版 Windows 机器上生成的文件。无论如何将这些文件编码为utf-8而不是生成的cp-1252?

回答by Eric Grunzke

You can read and write text data in any encoding that you wish. Here's a quick code example:

您可以按照您希望的任何编码读取和写入文本数据。这是一个快速的代码示例:

  public static void main(String[] args) throws Exception
  {
    // List all supported encodings
    for (String cs : Charset.availableCharsets().keySet())
      System.out.println(cs);

    File file = new File("SomeWindowsFile.txt");
    StringBuilder builder = new StringBuilder();

    // Construct a reader for a specific encoding
    Reader reader = new InputStreamReader(new FileInputStream(file), "windows-1252");
    while (reader.ready())
    {
      builder.append(reader.read());
    }
    reader.close();

    String string = builder.toString();

    // Construct a writer for a specific encoding
    Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8");
    writer.write(string);
    writer.flush();
    writer.close();
  }

If this still 'chokes' on read, see if you can verify that the the original encoding is what you think it is. In this case I've specified windows-1252, which is the java string for cp-1252.

如果这在读取时仍然“窒息”,请查看您是否可以验证原始编码是否与您认为的一样。在本例中,我指定了 windows-1252,它是 cp-1252 的 java 字符串。

回答by Joni

If the file names as well as content is a problem, the easiest way to solve the problem is setting the localeon the Linux machine to something based on ISO-8859-1rather than UTF-8. You can use locale -ato list available locales. For example if you have en_US.iso88591you could use:

如果文件名和内容都有问题,解决问题的最简单方法是将localeLinux 机器上的设置为基于ISO-8859-1而不是UTF-8. 您可以使用locale -a来列出可用的语言环境。例如,如果你有en_US.iso88591你可以使用:

export LANG=en_US.iso88591

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.encodingsystem property:

这样 Java 将使用 ISO-8859-1 作为文件名,这可能已经足够了。要运行 Java 程序,您仍然需要设置file.encoding系统属性:

java -Dfile.encoding=cp1252 -cp foo.jar:bar.jar blablabla

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:

如果没有可用的 ISO-8859-1 语言环境,您可以使用localedef. 但是安装它需要root访问权限。事实上,您可以生成一个使用 CP-1252 的语言环境,如果它在您的系统上可用的话。例如:

sudo localedef -f CP1252 -i en_US en_US.cp1252
export LANG=en_US.cp1252

This way Java should use CP1252 by default for all I/O, including file names.

这样,Java 应该默认为所有 I/O 使用 CP1252,包括文件名。

Expanded further here: http://jonisalonen.com/2012/java-and-file-names-with-invalid-characters/

在此处进一步扩展:http: //jonisalonen.com/2012/java-and-file-names-with-invalid-characters/