首页 技术 正文
技术 2022年11月20日
0 收藏 866 点赞 4,738 浏览 2039 个字

昨天突然下了个Java项目,把项目导入到eclipse中,发现项目是gbk编码格式想把项目变为utf-8,但是发现转换格式比较麻烦就写了这个代码,后面改进了下,想到说不定有人也需要就把它写了出来

代码如下

代码比较简单看懂了自己可以写一下,可以当做一个关于io流的一个练习

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 ylg
*/
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);
}
}
}}

写的不好的地方大家可以说一下共同学习!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295