【CSDN常见问题解答】目录文件编码转换
2013-12-13 23:23
411 查看
有时候导入一个工程到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;
}
}
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;
}
}
相关文章推荐
- Linux文件、目录、磁盘格式常见问题解答(鸟哥P160)
- 【CSDN常见问题解答】Java如何读取XML文件
- windows7下php5.4成功安装imageMagick,及解决php imagick常见错误问题。(phpinfo中显示不出来是因为:1.imagick软件本身、php本身、php扩展三方版本要一致,2.需要把CORE_RL_*.dll多个文件放到/php/目录下面)
- C# 路径,文件,目录,IO常见问题汇总
- 【CSDN常见问题解答】Java中JSON转XML
- C# 路径,文件,目录,IO常见问题汇总
- 【Redhat官方文件】红帽认证工程师及红帽认证技师常见问题解答
- 【CSDN常见问题解答】Java多种数据库连接
- 删除Oracle数据库时常见问题(注册表方面,文件目录方面,环境变量方面)
- 【CSDN常见问题解答】使用JNA调用Windows动态库
- FHS目录配置下,常见的几个问题及解答
- 【CSDN常见问题解答】Java多种数据库连接
- 【CSDN常见问题解答】Swing监听组合键
- 【Redhat官方文件】红帽认证工程师及红帽认证技师常见问题解答
- 【CSDN常见问题解答】用HTTPClient自动登录Tomcat管理页面
- C# 路径,文件,目录,IO常见问题汇总
- xml文件常见问题及解答
- 【CSDN常见问题解答】使用JNA调用Windows动态库
- C# 路径,文件,目录,IO常见问题汇总
- FHS目录配置下,常见的几个问题及解答