首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Eclipse 中文亂碼徹底解決:四層編碼設定優先級與實戰修復

     編輯:Java 相關知識

同一文件在 Eclipse 中以不同編碼開啟會產生亂碼。Eclipse 提供四層編碼配置,優先級由低到高:

  1. Workspace:Window → Preferences → General → Workspace → Text file encoding(預設跟隨作業系統)
  2. Project:專案右鍵 Properties → Resource → Text file encoding,預設勾選 "Inherited from container"
  3. Content Type:Preferences → General → Content Types → Text → Java Source File → Default encoding,手動輸入編碼後必須點擊 Update
  4. File:檔案右鍵 Properties → Resource → Text file encoding,單檔最高優先級

實戰修復流程

  1. 先確認檔案實際編碼(用 Notepad++/VS Code 開啟查看右下角)
  2. 按優先級從高到低排查:File → Content Type → Project → Workspace
  3. 將對應層級設定為檔案實際編碼
  4. 關閉重開檔案驗證

常見坑點

  • Content Type 設定後未點 Update 不生效
  • 專案勾選 "Inherited from container" 卻期望獨立編碼
  • JSP/HTML 頭部宣告 <%@ page contentType="text/html;charset=UTF-8" %> 僅影響瀏覽器渲染,不影響 Eclipse 編輯器解碼

一勞永逸方案

團隊統一:Workspace 設 UTF-8、專案鎖定 UTF-8、Content Type 主流類型全設 UTF-8、Git 強制 text=auto eol=lf。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved