




根本原因是file.encoding、project encoding和properties file encoding三者不一致且IDEA不自动同步,需统一设为UTF-8并勾选Transparent native-to-ascii conversion。
根本原因通常是 file.encoding、project encoding 和 properties file encoding 三者不一致,且 IDEA 默认不会自动同步。比如你改了全局编码为 UTF-8,但项目根目录下的 .idea/misc.xml 仍保留旧的 encoding="GBK",就会覆盖设置。
File → Settings → Editor → File Encodings,确认三项都设为 UTF-8(Global Encoding、Project Encoding、Default encoding for properties files)
.properties 文件里会变成 \u4f60\u597d 形式且无法反向识别Reload project from disk 或先用记事本另存为 UTF-8 再拖回 IDEA靠人眼校验或口头约定几乎必然失效。IDEA 支持导入/导出 code style 配置,本质是 XML 文件,可纳入 Git 管理。
Settings → Editor → Code Style → Java → Manage → Export,保存为 java-code-style.xml
Import Scheme → IntelliJ IDEA code style XML,选中团队配置文件
Method call arguments 换行策略(如是否强制每参数一行)、Wrap on typing 是否开启、Field annotations 是否换行——这些直接影响 PR 中 diff 的可读性Optimize imports on the fly 并勾选 Remove unused imports,避免手工删 import 遗漏触发格式化时,IDEA 实际执行的是当前激活的 Code Style 配置 + 当前文件的 file type 绑定规则。常见错配是 Java 文件被误识别为 Text 或 Auto-detect 失效。
Java;若显示 Plain Text,右键文件 → Override File Type → Java
Settings → Editor → Code Style → Java → Tabs and Indents,重点确认:Use tab character(通常关)、Tab size / Indent(建议统一为 4)、Continuation indent(建议 8)Enable annotation processing(Settings → Build → Compiler → Annotation Processors),否则 @Data 生成的 getter/setter 可能被格式化误伤IDEA 的编码设置只影响编辑器行为,不影响编译器。javac 默认使用系统 locale 编码读源码,若项目里 sourceCompatibility = 11 但源码含中文且未指定 -encoding UTF-8,编译就可能报 illegal character: \uFFFD。
pom.xml 的 maven-compiler-plugin 中显式声明:UTF-8
build.gradle 添加:compileJava.options.encoding = "UTF-8" 和 compileTestJava.options.encoding = "UTF-8"
gradle.properties,写入 org.gradle.jvmargs=-Dfile.encoding=UTF-8,确保 Gradle 进程自身也用 UTF-8真正麻烦的不是设哪一项,而是这四层(OS locale、IDEA encoding、build tool encoding、JVM args)任意一层脱节,都会导致中文字符在保存、编译、运行任一环节出问题。建议把 file.encoding=UTF-8 写进团队开发文档第一条。