java项目编码格式转换(如GBK转UTF-8)
来源:互联网 发布:开源电商平台 java 编辑:程序博客网 时间:2024/06/06 02:40
昨天突然下了个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); } } }}
写的不好的地方大家可以说一下共同学习!
阅读全文
0 0
- java项目编码格式转换(如GBK转UTF-8)
- PHP应用程序数据库编码转换(如GBK转UTF-8或UTF-8转GBK)注意事项
- iconv转换编码格式 gbk utf-8
- Ubuntu转换文件编码格式(gbk to utf-8 )
- gbk转utf-8 iconv 编码转换
- oracle编码格式从utf-8转换为GBK
- 字符串编码格式相互转换Unicode --UTF-8 -- GBK
- oracle编码格式从utf-8转换为GBK
- oracle编码格式从utf-8转换为GBK
- 如何使用Java代码将GBK编码格式的工程转换为UTF-8编码格式的工程
- eclipse转android studio解决GBK编码转换为UTF-8格式的方法
- java程序将GBK字符转成UTF-8编码格式
- java中的utf-8、gbk、ISO8859-1格式编码
- android 项目编码修改, GBK 变UTF-8(进阶版) 编码转换 项目代码转换 转utf-8 转换utf-8
- Java GBK,UTF-8编码
- java GBK字符转换成为UTF-8编码字符
- GBK编码数据转码为UTF-8格式
- 编码格式(关于utf-8,gb2312,gbk,big5等)
- POJ
- linux中的strip命令简介------给文件脱衣服
- 有关java多线程
- 一,零基础学习JavaScript-JavaScript简介(0)
- js检测当前使用的浏览器
- java项目编码格式转换(如GBK转UTF-8)
- LeetCode | 55. Jump Game
- 《程序员的自我修养》阅读笔记 --编译和链接
- Git常用操作总结
- HDU 1301 最小生成树,以及最小生成树的学习
- 动态代理
- RNA-seq与miRNA-seq联合分析
- SpringMVC 数据绑定
- codeforces 820B