【问题标题】:How to display Greek Character when running from cmd从cmd运行时如何显示希腊字符
【发布时间】:2019-01-19 06:01:00
【问题描述】:

我制作了一个程序,它可以抓取网站并提取文本并将其写入 .txt 文件。当我从 Intelij Idea 运行我的程序时,每一行都以希腊语正确打印。但是当我从 cmd 运行 jar 文件时,希腊文本被写成乱码。

public class Logger extends Thread{
String input,path;
int matchNumber;
public Logger(String path0) {
    path=path0;
}
public void log (int matchesNumber0,String input0) {
    matchNumber=matchesNumber0;
    input=input0;
    this.run();
}
@Override
public void run() {
    BufferedWriter textWriter = null;
    DateFormat date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date today = Calendar.getInstance().getTime();
    String stringDateToday = date.format(today);
    if(input!=null) {
        try {
            String logFilePath = path;
            textWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFilePath), StandardCharsets.UTF_8));
            textWriter.write(stringDateToday + "-----" + matchNumber + "-----");
            textWriter.write(input + "\n");
            if (!input.contains(" content=")) {
                setWarningMsg(input);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Incorrect Log File path!!!");
        } finally {
            try {
                textWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

cmd Output

【问题讨论】:

  • 您使用的是 MS Windows 吗?

标签: java cmd character-encoding fileoutputstream bufferedwriter


【解决方案1】:

您用来查看文件的文本编辑器可能无法正确解码希腊文本文件。

如果您在 Unix 上(或在 Windows 上使用 Cygwin),file 程序可能会有所帮助。该程序查看前几个字节以尝试猜测适当打开文件的程序。或者尝试在Intellij 本身中打开文件。

【讨论】:

    【解决方案2】:
    chcp 65001 && java -jar -Dfile.encoding=UTF-8 path/to/your/runnable/jar
    

    这解决了我的问题!

    【讨论】:

    • 将cmd输出添加为图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多