add linux java乱码
134
ubuntu/Linux 下解决 Java 输出文件,中文变问号和中文乱码问题.md
Normal file
@ -0,0 +1,134 @@
|
||||
> 本文由 [简悦 SimpRead](http://ksria.com/simpread/) 转码, 原文地址 [blog.csdn.net](https://blog.csdn.net/weixin_43890033/article/details/122131197)
|
||||
|
||||
### 目录
|
||||
|
||||
* [安装字库](#_9)
|
||||
* * [安装中文字体](#_16)
|
||||
* [激活中文字库](#_31)
|
||||
* [设置中文环境](#_48)
|
||||
* [最终效果](#_76)
|
||||
|
||||
Linux 下 Java 输出带中文的文件,出现问号或者中文[乱码](https://so.csdn.net/so/search?q=%E4%B9%B1%E7%A0%81&spm=1001.2101.3001.7020)问题:
|
||||
![](assets/5f05716ed04f48c080981fbe3a2f76ce.png)
|
||||
解决方法很简单,大概分为两个步骤:安装和设置字库、设置语言环境变量。最终效果:
|
||||
![](assets/f1217a97bebf47af847b262e8c6af524.png)
|
||||
需要注意:
|
||||
|
||||
* 已经由 Java 等程序输出的文件(`???`文件名),因为是生成文件时,缺少字符集;添加和更改中文后,还是显示`???`。
|
||||
* 一些教程会让你直接修改`/etc/default/locale`,但是个人不建议修改这个文件为`zh`。
|
||||
|
||||
安装字库
|
||||
====
|
||||
|
||||
首先,看看系统有没有中文字库:
|
||||
|
||||
```
|
||||
locale -a | grep zh_CN
|
||||
|
||||
```
|
||||
|
||||
![](assets/6b9f1fd3cbae498f81bc734c0e1e513a.png)
|
||||
如果你没有中文字库,需要先安装中文字体。
|
||||
|
||||
安装中文字体
|
||||
------
|
||||
|
||||
安装中文字体很简单:
|
||||
|
||||
```
|
||||
# Debian
|
||||
sudo apt install fonts-wqy-zenhei
|
||||
# CentOS
|
||||
sudo yum install kde-l10n-Chinese
|
||||
|
||||
```
|
||||
|
||||
一般上述字体就足够使用,如果你在后文配置后,依旧无法使用,可以尝试安装全部 wqy 字体:
|
||||
|
||||
```
|
||||
# Debian
|
||||
sudo apt install xfonts-intl-chinese
|
||||
sudo apt install wqy\*
|
||||
|
||||
```
|
||||
|
||||
![](assets/c40ba4901cf64719a342fec89de50329.png)
|
||||
|
||||
激活中文字库
|
||||
------
|
||||
|
||||
之后,激活中文字体,CentOS 一般不需要激活中文字库:
|
||||
|
||||
```
|
||||
# Debian
|
||||
sudo apt install locales
|
||||
sudo dpkg-reconfigure locales
|
||||
|
||||
```
|
||||
|
||||
之后,就会进行语言选择:
|
||||
![](assets/2aa05b0bdc0f4fe6b6e87cb6f5be4132.png)
|
||||
通过使用`方向键`来浏览列表,并使用`空格`来勾选激活区域设置。这里建议勾选`en_US.UTF-8`、`zh_CN. UTF-8 URF-8` 等:
|
||||
![](assets/634f96ea28654d0eb785870dbff30e66.png)
|
||||
之后,回车:
|
||||
选择默认系统语言,还是推荐用英文:
|
||||
![](assets/dfdd550aa39645fbb6e4af83dad2b173.png)
|
||||
之后,再回车。系统会自动开始配置:
|
||||
![](assets/fd6ca3f8abcc4cd99496126c49125d1c.png)
|
||||
|
||||
> 很多文章,会直接让你在这里直接设置为`zh_CN.UTF-8`,我个人不建议这样设置。灵活性有点低,个人建议设置到用户的环境变量(见下文)。
|
||||
|
||||
设置中文环境
|
||||
======
|
||||
|
||||
现在,我们就可以设置中文环境了。也很简单,根据你当前的 Shell,设置环境变量,查看当前 Shell:
|
||||
|
||||
```
|
||||
echo $SHELL
|
||||
|
||||
```
|
||||
|
||||
![](assets/fe128a2af10140af9a57262bc83636a5.png)
|
||||
可以看到,我的 Shell 是`zsh`,你们的可能是`/bin/bash`,如果是和我一样,那么环境变量就是: `~/.zshrc`。如果是`/bin/bash`,那么环境变量就是:`~/.bashrc`。
|
||||
|
||||
```
|
||||
# 编辑环境变量
|
||||
vim ~/.zshrc
|
||||
|
||||
```
|
||||
|
||||
![](assets/bc18570392904484b79e6643af4d2495.png)
|
||||
|
||||
之后,在文末追加:
|
||||
|
||||
```
|
||||
# LANG
|
||||
export LANG=zh_CN.UTF-8
|
||||
export LC_ALL=zh_CN.UTF-8
|
||||
export LC_LANG=zh_CN.UTF-8
|
||||
|
||||
```
|
||||
|
||||
![](assets/01e4de9a92894823926700f8d7527f5b.png)
|
||||
最后,保存并退出后。重载环境变量:
|
||||
|
||||
```
|
||||
source ~/.zshrc
|
||||
|
||||
```
|
||||
|
||||
> 如果你不会`vim`命令操作,可以查看文章教程:[终端文本编辑神器–Vim 命令详解。如何配置 Vim 以及 Vim 插件?](https://www.mintimate.cn/2021/08/25/vim/)
|
||||
|
||||
现在,就可以查看效果了。
|
||||
|
||||
最终效果
|
||||
====
|
||||
|
||||
现在,我们的 Java 程序 ,写入文件名时候,就可以写中文了:
|
||||
![](assets/b3bae3ffc05d4eeb90f411e63b0bd770.png)
|
||||
但是,需要注意:
|
||||
|
||||
* 已经运行的 Java 程序,需要重新运行。
|
||||
* 一些虚拟终端,如:`screen`;需要关闭后重新打开。
|
||||
|
||||
如果还是没有生效,可以重启 Linux。
|
BIN
ubuntu/assets/01e4de9a92894823926700f8d7527f5b.png
Normal file
After Width: | Height: | Size: 204 KiB |
BIN
ubuntu/assets/2aa05b0bdc0f4fe6b6e87cb6f5be4132.png
Normal file
After Width: | Height: | Size: 200 KiB |
BIN
ubuntu/assets/5f05716ed04f48c080981fbe3a2f76ce.png
Normal file
After Width: | Height: | Size: 167 KiB |
BIN
ubuntu/assets/634f96ea28654d0eb785870dbff30e66.png
Normal file
After Width: | Height: | Size: 209 KiB |
BIN
ubuntu/assets/6b9f1fd3cbae498f81bc734c0e1e513a.png
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
ubuntu/assets/b3bae3ffc05d4eeb90f411e63b0bd770.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
ubuntu/assets/bc18570392904484b79e6643af4d2495.png
Normal file
After Width: | Height: | Size: 218 KiB |
BIN
ubuntu/assets/c40ba4901cf64719a342fec89de50329.png
Normal file
After Width: | Height: | Size: 329 KiB |
BIN
ubuntu/assets/dfdd550aa39645fbb6e4af83dad2b173.png
Normal file
After Width: | Height: | Size: 198 KiB |
BIN
ubuntu/assets/f1217a97bebf47af847b262e8c6af524.png
Normal file
After Width: | Height: | Size: 192 KiB |
BIN
ubuntu/assets/fd6ca3f8abcc4cd99496126c49125d1c.png
Normal file
After Width: | Height: | Size: 207 KiB |
BIN
ubuntu/assets/fe128a2af10140af9a57262bc83636a5.png
Normal file
After Width: | Height: | Size: 40 KiB |