java - ConEmu (CMD) 无法识别这些 un​​icode 字符?

标签 java conemu

public void print(boolean nextLine, int delayInt, String load) throws UnsupportedEncodingException {
    PrintStream p = new PrintStream(System.out, true,"UTF-8");
    for(int x = 0; x < load.length(); x++ ){
        p.print(load.charAt(x));
        try{
            Thread.sleep(delayInt);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    if(nextLine)
        System.out.println();
}

好吧,上面是打印字符串中每个字符的方法,我使用 PrintStream 是因为我觉得当我在 ConEmu 中编译和执行 Java 程序时,它允许我使用所有 Unicode 字符(我正在使用它而不是CMD @ Windows)

i.print(true, 50,"\n" +
            "\n" +
            "▓█████▄         ██▓         ██████         ▄████▄          ▒█████          ██▒   █▓       ▓█████         ██▀███         \n" +
            "▒██▀ ██▌       ▓██▒       ▒██    ▒        ▒██▀ ▀█         ▒██▒  ██▒       ▓██░   █▒       ▓█   ▀        ▓██ ▒ ██▒       \n" +
            "░██   █▌       ▒██▒       ░ ▓██▄          ▒▓█    ▄        ▒██░  ██▒        ▓██  █▒░       ▒███          ▓██ ░▄█ ▒       \n" +
            "░▓█▄   ▌       ░██░         ▒   ██▒       ▒▓▓▄ ▄██▒       ▒██   ██░         ▒██ █░░       ▒▓█  ▄        ▒██▀▀█▄         \n" +
            "░▒████▓        ░██░       ▒██████▒▒       ▒ ▓███▀ ░       ░ ████▓▒░          ▒▀█░         ░▒████▒       ░██▓ ▒██▒       \n" +
            " ▒▒▓  ▒        ░▓         ▒ ▒▓▒ ▒ ░       ░ ░▒ ▒  ░       ░ ▒░▒░▒░           ░ ▐░         ░░ ▒░ ░       ░ ▒▓ ░▒▓░       \n" +
            " ░ ▒  ▒         ▒ ░       ░ ░▒  ░ ░         ░  ▒            ░ ▒ ▒░           ░ ░░          ░ ░  ░         ░▒ ░ ▒░       \n" +
            " ░ ░  ░         ▒ ░       ░  ░  ░         ░               ░ ░ ░ ▒              ░░            ░            ░░   ░        \n" +
            "   ░            ░               ░         ░ ░                 ░ ░               ░            ░  ░          ░            \n" +
            " ░                                        ░                                    ░                                        \n" +
            "\n"
    );
    i.print(true, 2, "\n" +
            "\n" +
            " ▄█         ▄████████                                                                                                                                       \n" +
            "███        ███    ███                                                                                                                                       \n" +
            "███▌       ███    ███                                                                                                                                       \n" +
            "███▌       ███    ███                                                                                                                                       \n" +
            "███▌     ▀███████████                                                                                                                                       \n" +
            "███        ███    ███                                                                                                                                       \n" +
            "███        ███    ███                                                                                                                                       \n" +
            "█▀         ███    █▀                                                                                                                                        \n" +
            "                                                                                                                                                            \n" +
            " ▄█   ▄█   ▄█      ▀█████████▄         ▄████████      ▄█       ▄████████        ▄█   ▄█▄      ▄█   ▄█   ▄█                                                  \n" +
            "███  ███  ███        ███    ███       ███    ███     ███      ███    ███       ███ ▄███▀     ███  ███  ███                                                  \n" +
            "███▌ ███▌ ███▌       ███    ███       ███    ███     ███▌     ███    █▀        ███▐██▀       ███▌ ███▌ ███▌                                                 \n" +
            "███▌ ███▌ ███▌      ▄███▄▄▄██▀       ▄███▄▄▄▄██▀     ███▌     ███             ▄█████▀        ███▌ ███▌ ███▌                                                 \n" +
            "███▌ ███▌ ███▌     ▀▀███▀▀▀██▄      ▀▀███▀▀▀▀▀       ███▌     ███            ▀▀█████▄        ███▌ ███▌ ███▌                                                 \n" +
            "███  ███  ███        ███    ██▄     ▀███████████     ███      ███    █▄        ███▐██▄       ███  ███  ███                                                  \n" +
            "███  ███  ███        ███    ███       ███    ███     ███      ███    ███       ███ ▀███▄     ███  ███  ███                                                  \n" +
            "█▀   █▀   █▀       ▄█████████▀        ███    ███     █▀       ████████▀        ███   ▀█▀     █▀   █▀   █▀                                                   \n" +
            "                                      ███    ███                               ▀                                                                            \n" +
            " ▄█   ▄█   ▄█      ▀█████████▄         ▄████████        ▄████████        ▄████████        ▄█   ▄█▄        ▄████████        ▄████████      ▄█   ▄█   ▄█      \n" +
            "███  ███  ███        ███    ███       ███    ███       ███    ███       ███    ███       ███ ▄███▀       ███    ███       ███    ███     ███  ███  ███      \n" +
            "███▌ ███▌ ███▌       ███    ███       ███    ███       ███    █▀        ███    ███       ███▐██▀         ███    █▀        ███    ███     ███▌ ███▌ ███▌     \n" +
            "███▌ ███▌ ███▌      ▄███▄▄▄██▀       ▄███▄▄▄▄██▀      ▄███▄▄▄           ███    ███      ▄█████▀         ▄███▄▄▄          ▄███▄▄▄▄██▀     ███▌ ███▌ ███▌     \n" +
            "███▌ ███▌ ███▌     ▀▀███▀▀▀██▄      ▀▀███▀▀▀▀▀       ▀▀███▀▀▀         ▀███████████     ▀▀█████▄        ▀▀███▀▀▀         ▀▀███▀▀▀▀▀       ███▌ ███▌ ███▌     \n" +
            "███  ███  ███        ███    ██▄     ▀███████████       ███    █▄        ███    ███       ███▐██▄         ███    █▄      ▀███████████     ███  ███  ███      \n" +
            "███  ███  ███        ███    ███       ███    ███       ███    ███       ███    ███       ███ ▀███▄       ███    ███       ███    ███     ███  ███  ███      \n" +
            "█▀   █▀   █▀       ▄█████████▀        ███    ███       ██████████       ███    █▀        ███   ▀█▀       ██████████       ███    ███     █▀   █▀   █▀       \n" +
            "                                      ███    ███                                         ▀                                ███    ███                        \n" +
            " ▄█   ▄█   ▄█         ▄████████        ▄████████     ████████▄       ███    █▄         ▄████████      ▄█            ▄█   ▄█   ▄█                            \n" +
            "███  ███  ███        ███    ███       ███    ███     ███    ███      ███    ███       ███    ███     ███           ███  ███  ███                            \n" +
            "███▌ ███▌ ███▌       ███    █▀        ███    █▀      ███    ███      ███    ███       ███    █▀      ███           ███▌ ███▌ ███▌                           \n" +
            "███▌ ███▌ ███▌       ███             ▄███▄▄▄         ███    ███      ███    ███      ▄███▄▄▄         ███           ███▌ ███▌ ███▌                           \n" +
            "███▌ ███▌ ███▌     ▀███████████     ▀▀███▀▀▀         ███    ███      ███    ███     ▀▀███▀▀▀         ███           ███▌ ███▌ ███▌                           \n" +
            "███  ███  ███               ███       ███    █▄      ███    ███      ███    ███       ███    █▄      ███           ███  ███  ███                            \n" +
            "███  ███  ███         ▄█    ███       ███    ███     ███  ▀ ███      ███    ███       ███    ███     ███▌    ▄     ███  ███  ███                            \n" +
            "█▀   █▀   █▀        ▄████████▀        ██████████      ▀██████▀▄█     ████████▀        ██████████     █████▄▄██     █▀   █▀   █▀                             \n" +
            "                                                                                                     ▀                                                      \n" +
            " ▄█                                                                                                                                                         \n" +
            "███                                                                                                                                                         \n" +
            "███▌                                                                                                                                                        \n" +
            "███▌                                                                                                                                                        \n" +
            "███▌                                                                                                                                                        \n" +
            "███                                                                                                                                                         \n" +
            "███                                                                                                                                                         \n" +
            "█▀                                                                                                                                                          \n" +
            "                                                                                                                                                            \n" +
            "\n" +
            " \n");

但是,在 IDE 上,它确实可以运行,但在 ConEmu 上它显示, “testing.java:50: 错误: 无法映射的字符 (0x90) 用于编码 windows-1252” 错误 我已经检查了解决方案,但它们仅适用于当我有 '\uXXXX' 字符代码时,在这种情况下我没有我使用了在线 ASCII 艺术生成器。 ASCII ART Generator which I have used

最佳答案

根据ConEmu documentation :

UTF-8 (UTF8)

On Windows, unlike Unix, the console itself is not a stream of ‘bytes’ but a spreadsheet of cells, each of which contains an UTF-16 character and a color attribute.

That means if your application produces output using single-byte or multi-byte character sets (which are ANSI, OEM, UTF-8 and many others) the Windows converts that output to UTF-16 automatically regarding active codepage selected in your console (run chcp from console command prompt to check your active codepage).

If you want to work with UTF-8 encoding you have to select UTF-8 as active console codepage. Just run chcp 65001 command to do that. You may call this command from your shell prompt or run it before shell in a ConEmu task content. Of course your application must be able to output data using UTF-8. Refer to your application manual.

只需输入您的 ConEmu 终端,您就可以看到 UTF-8 内容:

chcp 65001

您还必须稍微修改一下源代码并将其另存为 UTF-8 文件:

public void print(boolean nextLine, int delayInt, String load) throws UnsupportedEncodingException {
    for(int x = 0; x < load.length(); x++ ){

        // Use the regular System.out.print here to output chars!
        System.out.print(load.charAt(x));

        try{
            Thread.sleep(delayInt);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    if(nextLine)
        System.out.println();
}

编译运行脚本:

chcp 65001
java test

我在 ConEmu 控制台中得到的结果: enter image description here

关于java - ConEmu (CMD) 无法识别这些 un​​icode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54117211/

相关文章:

windows - 巴布恩 : copy/paste from windows clipboard into vim?

java - java IText 中不存在 com.itextpdf.text.pdf.PdfPKCS7 库

java - 从一个 java 文件读取并写入另一个文件

python - Conemu - 重用实例,但不将其带到前台

python - 多个 conemu 窗口 - 如何区分?

c++ - SetProgressValue() 在 ConEmu 中不起作用

java - 当字符串形状非法时抛出异常

适用于 AIX 的 Java 8 选项

java - 如果在java中使用smack Api和openfire,可以显示powerpoint并在客户端聊天中滑动它

windows - Cmder bash 脚本执行