在写项目的过程中我发现有的地方编码格式被设置成了
gbk如果用eclipse等工具直接改回utf-8编码格式则会出现乱码。
在这里搞了一个工具,直接输入之前的编码格式跟要改的编码格式就会自动转换

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

以下是源代码:
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);
}
}
}
}
到此这篇关于java工程编码格式由gbk转化成utf-8的具体实现的文章就介绍到这了,更多相关java编码格式gbk转utf-8内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论