在写项目的过程中我发现有的地方编码格式被设置成了 gbk 如果用eclipse等工具直接改回utf-8编码格式则会出现乱码。

下载:https://download.csdn.net/download/weixin_44893902/20367405

在这里搞了一个工具,直接输入之前的编码格式跟要改的编码格式就会自动转换


转换完成后直接设置为更改后的格式即可

以下是源代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner; /**
* 把gbk编码的程序变换为用utf-8的格式编码
*
* 此程序只是为了改变 .java文件的编码格式如果你想要变换为其他格式只需要改变下面对应的编码按格式
*
* @author 明金同学 CSDN:https://ymjin.blog.csdn.net/
*/
public class Files { /**
*
* @param args
* @throws UnsupportedEncodingException
* @throws IOException
*/
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
Scanner scan = new Scanner(System.in);
System.out.println("请输入需要改变编码格式的文件位置");
String str = scan.nextLine();
File file = new File(str);
System.out.println("文件的初始编码");
String bm1 = scan.nextLine();
System.out.println("文件需要转换成的编码");
String bm2 = scan.nextLine();
getAllFiles(file, bm1, bm2);
} /**
*
* @param file 要编译的文件
* @param bm1 文件的初始编码
* @param bm2 文件需要转换成的编码
* @throws FileNotFoundException 文件找不到
* @throws UnsupportedEncodingException 编码出错
* @throws IOException io异常
*/
public static void getAllFiles(File file, String bm1, String bm2) throws FileNotFoundException, UnsupportedEncodingException, IOException {
if (file.isDirectory()) {
File[] test = file.listFiles();
for (File test1 : test) {
//类的名字
String str = test1.getPath();
if (str.endsWith("java") & test1.isFile()) {
String[] s = str.split("\\.");
String filecope = s[0] + "cope." + s[1];
System.out.println(filecope);
File fil = new File(filecope);
//转格式
InputStreamReader isr = new InputStreamReader(new FileInputStream(test1), bm1);
OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream(fil), bm2);
int re = -1;
while ((re = isr.read()) != -1) {
osr.write(re);
}
isr.close();
osr.close();
InputStreamReader isrr = new InputStreamReader(new FileInputStream(fil), bm2);
OutputStreamWriter osrw = new OutputStreamWriter(new FileOutputStream(test1), bm2);
int r = -1;
while ((r = isrr.read()) != -1) {
osrw.write(r);
}
isrr.close();
osrw.close();
boolean d = fil.delete();
System.out.println(str + "文件转换utf-8成功:" + d);
}
getAllFiles(test1, bm1, bm2);
}
}
} }

参考:
将GBK项目修改成UTF-8项目
https://blog.csdn.net/chengzhuo5471/article/details/100840187
https://my.oschina.net/momomo/blog/845610

最新文章

  1. Sublime Text 解决中文乱码
  2. Core functionality.md
  3. jQuery 简单过滤选择器
  4. 51nod 1163 最高的奖励(贪心+优先队列)
  5. baidu地图的一个拾取坐标系统
  6. Servlet中的过滤器Filter用法
  7. jenkins+webhook+docker做持续集成
  8. Hibernate学习笔记(4)---hibernate的核心接口
  9. 华科机考:N阶楼梯上楼
  10. DOS界面下的翻译软件制作
  11. app后端设计(14)--LBS的偏移问题
  12. jar文件和aar文件的区别
  13. SpringBoot使用JSP(官网Demo)
  14. 移动端 上传头像 并裁剪功能(h5)
  15. react路由
  16. C#读取wav文件
  17. 【.NET线程--进阶(一)】--线程方法详解
  18. 10.7-uC/OS-III内部任务(定时器任务 OS_TmrTask())
  19. Java多线程之CountDownLatch和CyclicBarrier同步屏障的使用
  20. 协议 protocol

热门文章

  1. Could not get a resource from the pool
  2. tomcat 之 httpd session stiky
  3. d3动态坐标轴
  4. windows 查看端口被占用,解除占用
  5. 使用Stream方式处理集合元素
  6. 11.Vue.js-事件处理器
  7. 关于synchronize与lock的区别
  8. podman wsl2在windows重启后出错
  9. 一个使用 asyncio 开发的网络爬虫(译文)
  10. 象群游牧算法--EHO