You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java字符串特殊字符无法显示问题求助(IntelliJ环境)

问题描述

在IntelliJ中编写Java示例时,字符串中的特殊字符(如ç、ü、ğ)无法正常显示,输出时变成�。

定义的Team类:

public class Team {

    private String name;
    private int points;
    private int goalsScored;
    private int goalsAgainst;
    private int goalDifference;
}

赋值代码:

private static List getTeamStats() {
    List plTeams = new ArrayList<>();
    plTeams.add(new Team("Team ç", 33, 27, 12));
    plTeams.add(new Team("Team ü", 32, 40, 16));
    plTeams.add(new Team("Team ğ", 30, 24, 16));
    return plTeams;
}

使用plTeams.forEach(System.out::println);遍历输出时,结果显示为:

Team �  ...
Team �  ...
Team �  ...

已尝试的方法:

  • 调用Locale.setDefault(Locale.forLanguageTag("tr-TR"));
  • 在Help→Edit Custom VM Options中添加:
    -Duser.language=tr
    -Duser.country=TR
    -Duser.variant=TR
    

但均未解决问题。

解决方案

这个问题本质是控制台输出编码不匹配导致的,和Locale设置关联不大,按以下步骤处理:

1. 配置IntelliJ文件及控制台编码

  • 打开IntelliJ设置(Windows/Linux:File → Settings;Mac:IntelliJ IDEA → Settings)
  • 导航到Editor → File Encodings
  • 将以下编码全部设置为UTF-8:
    • Global Encoding
    • Project Encoding
    • Default encoding for properties files
  • 点击Apply保存设置

2. 修改VM参数指定编码

  • 打开Help → Edit Custom VM Options
  • 添加以下两行参数(可保留原Locale参数,或替换):
    -Dfile.encoding=UTF-8
    -Dconsole.encoding=UTF-8
    
  • 重启IntelliJ使参数生效

3. 确保Team类正确实现toString方法

默认的toString方法无法正确输出自定义类的字符串属性,需补充重写:

@Override
public String toString() {
    return "Team{" +
            "name='" + name + '\'' +
            ", points=" + points +
            ", goalsScored=" + goalsScored +
            ", goalsAgainst=" + goalsAgainst +
            ", goalDifference=" + goalDifference +
            '}';
}

4. 检查系统终端编码(可选)

如果使用外部终端而非IntelliJ内置控制台,需确保系统终端编码为UTF-8:

  • Windows:在终端执行chcp 65001切换编码
  • Mac/Linux:在终端执行export LC_ALL=tr_TR.UTF-8设置土耳其语UTF-8 locale

内容的提问来源于stack exchange,提问作者Sercan Noyan Germiyanoğlu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:25:33