首页 > 解决方案 > 从cmd运行时如何显示希腊字符

问题描述

我制作了一个程序,它可以抓取网站并提取文本并将其写入 .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();
            }
        }
    }
}

命令输出

标签: javacmdcharacter-encodingfileoutputstreambufferedwriter

解决方案


chcp 65001 && java -jar -Dfile.encoding=UTF-8 path/to/your/runnable/jar

这解决了我的问题!


推荐阅读