- dali
- 等级: 初级会员
- 性别:
- 文章: 2
- 积分: 30
- 来自: 南京
|
目录文件编码转换
java 代码
- package com.xw.file;
-
- 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) {
-
- if (pathname.isDirectory()
- || (pathname.isFile() && pathname.getName().endsWith(
- ".java")))
- return true;
- else
- return false;
- }
- };
-
-
-
-
- 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();
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- public static void convert(String infile, String outfile, String from,
- String to) throws IOException, UnsupportedEncodingException {
-
- 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;
-
-
- if (from == null)
- from = System.getProperty("file.encoding");
- if (to == null)
- to = System.getProperty("file.encoding");
-
-
- Reader r = new BufferedReader(new InputStreamReader(in, from));
- Writer w = new BufferedWriter(new OutputStreamWriter(out, to));
-
-
-
-
-
-
-
- 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;
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|