将命令行 unicode 参数传递给 Java 代码 [英] Passing command line unicode argument to Java code

查看:17
本文介绍了将命令行 unicode 参数传递给 Java 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将日语的命令行参数传递给 Java 主方法.如果我在命令行窗口中输入 Unicode 字符,它会显示 '?????'没关系,但是传递给java程序的值也是'?????'.如何获取命令窗口传递的参数的正确值?下面是将命令行参数提供的值写入文件的示例程序.

I have to pass command line argument which is Japanese to Java main method. If I type Unicode characters on command-line window, it displays '?????' which is OK, but the value passed to java program is also '?????'. How do I get the correct value of argument passed by the command window? Below is sample program which writes to a file the value supplied by command line argument.

public static void main(String[] args) {
        String input = args[0];
        try {
            String filePath = "C:/Temp/abc.txt";
            File file = new File(filePath);
            OutputStream out = new FileOutputStream(file);
            byte buf[] = new byte[1024];
            int len;
            InputStream is = new ByteArrayInputStream(input.getBytes());
            while ((len = is.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

推荐答案

不幸的是,您不能可靠地将非 ASCII 字符与使用 Windows C 运行时标准库的命令行应用程序一起使用,例如 Java(以及几乎所有非 Windows-确实是特定的脚本语言).

Unfortunately you cannot reliably use non-ASCII characters with command-line apps that use the Windows C runtime's stdlib, like Java (and pretty much all non-Windows-specific scripting languages really).

这是因为它们默认使用特定于语言环境的代码页读取输入和输出,这绝不是 UTF,与使用 UTF-8 的其他现代操作系统不同.

This is because they read their input and output using a locale-specific code page by default, which is never a UTF, unlike every other modern OS which uses UTF-8.

虽然您可以使用 chcp 命令将终端的代码页更改为其他内容,但 chcp 65001 下对 UTF-8 编码的支持在一个几种可能导致应用程序崩溃的方法.

Whilst you can change the code page of a terminal to something else using the chcp command, the support for the UTF-8 encoding under chcp 65001 is broken in a few ways that are likely to trip apps up fatally.

如果您只需要日语,您可以通过将区域设置(区域设置中的非 Unicode 应用程序的语言")设置为日本来切换到代码页 932(类似于 Shift-JIS).对于不在该代码页中的字符,这仍然会失败.

If you only need Japanese you could switch to code page 932 (similar to Shift-JIS) by setting your locale (‘language for non-Unicode applications’ in the Regional settings) to Japan. This will still fail for characters that aren't in that code page though.

如果需要在Windows上可靠地通过命令行获取非ASCII字符,则需要直接调用Win32 API函数GetCommandLineW,避开encode-to-system-code-page层.可能您想使用 JNA 来做到这一点.

If you need to get non-ASCII characters through the command line reliably on Windows, you need to call the Win32 API function GetCommandLineW directly to avoid the encode-to-system-code-page layer. Probably you'd want to do that using JNA.

这篇关于将命令行 unicode 参数传递给 Java 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆