【CSDN常见问题解答】目录文件编码转换
来源:互联网 发布:淘宝网首页无尘服 编辑:程序博客网 时间:2024/05/01 17:36
有时候导入一个工程到Eclipse中发现中文乱码了。原因就是工程的编码格式变了。比如从UTF-8变成了GBK等等。此时我们手动去改的话十分麻烦。下面这个类就是用来改变整个目录的编码的,详细请看代码。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.io.Writer; public class FileEncodeConverter { // 原文件目录 private static String srcDir = "x:/src"; // 转换后的存放目录 private static String desDir = "x:/des"; // 源文件编码 private static String srcEncode = "gb2312"; // 输出文件编码 private static String desEncode = "utf-8"; // 处理的文件过滤 private static FileFilter filter = new FileFilter() { public boolean accept(File pathname) { // 只处理:目录 或是 .java文件 if (pathname.isDirectory() || (pathname.isFile() && pathname.getName().endsWith( ".java"))) return true; else return false; } }; /** * @param file */ public static void readDir(File file) { File[] files = file.listFiles(filter); for (File subFile : files) { // 建立目标目录 if (subFile.isDirectory()) { File file3 = new File(desDir + subFile.getAbsolutePath().substring(srcDir.length())); if (!file3.exists()) { file3.mkdir(); } file3 = null; readDir(subFile); } else { System.err.println("一源文件:\t"+subFile.getAbsolutePath() + "\n目标文件:\t" + (desDir + subFile.getAbsolutePath().substring(srcDir.length()))); System.err.println("-----------------------------------------------------------------"); try { convert(subFile.getAbsolutePath(), desDir + subFile.getAbsolutePath().substring(srcDir.length()), srcEncode, desEncode); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * @param infile 源文件路径 * @param outfile 输出文件路径 * @param from 源文件编码 * @param to 目标文件编码 * @throws IOException * @throws UnsupportedEncodingException */ public static void convert(String infile, String outfile, String from, String to) throws IOException, UnsupportedEncodingException { // set up byte streams InputStream in; if (infile != null) in = new FileInputStream(infile); else in = System.in; OutputStream out; if (outfile != null) out = new FileOutputStream(outfile); else out = System.out; // Use default encoding if no encoding is specified. if (from == null) from = System.getProperty("file.encoding"); if (to == null) to = System.getProperty("file.encoding"); // Set up character stream Reader r = new BufferedReader(new InputStreamReader(in, from)); Writer w = new BufferedWriter(new OutputStreamWriter(out, to)); // Copy characters from input to output. The InputStreamReader // converts from the input encoding to Unicode,, and the // OutputStreamWriter // converts from Unicode to the output encoding. Characters that cannot // be // represented in the output encoding are output as '?' char[] buffer = new char[4096]; int len; while ((len = r.read(buffer)) != -1) w.write(buffer, 0, len); r.close(); w.flush(); w.close(); } public static void main(String[] args) { // 建立目标文件夹 File desFile = new File(desDir); if (!desFile.exists()) { desFile.mkdir(); } desFile = null; File srcFile = new File(srcDir); // 读取目录 循环转换文件 readDir(srcFile); srcFile = null; } }
1 0
- 【CSDN常见问题解答】目录文件编码转换
- 目录文件编码转换
- 【CSDN常见问题解答】JSON和XML格式互相转换
- 【CSDN常见问题解答】Java如何读取XML文件
- Linux文件、目录、磁盘格式常见问题解答(鸟哥P160)
- xml文件常见问题及解答
- 流媒体文件应用常见问题解答
- 【CSDN常见问题解答】Java多种数据库连接
- 【CSDN常见问题解答】Swing监听组合键
- 【CSDN常见问题解答】Javap 的使用
- 【CSDN常见问题解答】Java多种数据库连接
- ESN向MEID转换常见问题解答
- 操作文件和目录解答
- linux 下转换指定目录下所有文件字符编码
- 文件编码批量转换——转换一个目录中所有文本文件的编码
- 常见问题解答
- 常见问题解答
- 【CSDN常见问题解答】Java单例模式分析
- SSLPeerUnverified peer not authenticated
- 利用ycsb测试redis性能
- System/App和Data/App的区别
- 第三章 数据库和表 命令行操作记录
- Hadoop 新 MapReduce 框架 Yarn 详解
- 【CSDN常见问题解答】目录文件编码转换
- android应用没有Internet的访问权限怎样向服务器提交数据
- proguard for google gson
- Git 远程仓库——关联和push和clone
- 英语6级复习的好资料
- 服务器并发处理能力
- HTML Basic I
- 第16周项目4 判断两个有序数组中是否含有相同数字
- 自己组装摄像头软件opencv无法打开的问题