www.pudn.com > charsettools_1.0.2.zip > FileConverter.java
/*Program Converter.java *
Author Keiven Ju *
Created 2003-12-15 9:38:55 *
Copyright 1995-1998,2000-2003 by AKuP International, Inc.,
*
Nanjing:
* Unit C, 26F Jianjiang Culture Building, No. 89 Zhongshannan Rd. Nanjing 210005 China
*
26F, Jianjiang Building No.89,Zhongshan South Rd, Nanjing *
Taipei :
*
5F, NO.6, Alley 36, Lane 26, Rueiguang Rd, Neihu District, Taipei City 114, Taiwan *
* All rights reserved.
*
* This software is the confidential and proprietary information
* of AKuP International, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with AKuP.
*
* E-mail keiven.ju@akup.com for more information. */ package com.akup.charset; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; /** * program Converter * @author Keiven Ju * @since 1.0.0 * @version 1.0.0 2003-12-15 9:38:55 */ public class FileConverter { private CharsetToolkit chtoolkit = new CharsetToolkit(); /** * convertFile, * @param srcfilename * @param srcCharset * @param trgfilename * @param trgCharset * @throws IOException * @since 1.0.0 2003-12-16 */ public void convertFile(String srcfilename, String srcCharset, String trgfilename, String trgCharset) throws IOException { File srcdir = new File(srcfilename); File trgdir = new File(trgfilename); if (!srcdir.exists() || !srcdir.isFile()) throw new FileNotFoundException("Not found file '" + srcdir.getAbsolutePath() + "'"); chtoolkit.convertFile(srcdir, srcCharset, trgdir, trgCharset); } /** * convertDirectory,转换整个目录, * @param srcpath 源目录 * @param srcCharset 源字符编码 * @param trgpath 目标目录 * @param trgCharset 目标字符编码 * @throws IOException * @since 1.0.0 2003-12-16 */ public void convertDirectory(String srcpath, String srcCharset, String trgpath, String trgCharset, String filte, boolean includechild) throws IOException { File srcdir = new File(srcpath); File trgdir = new File(trgpath); if (!srcdir.exists() || !srcdir.isDirectory()) throw new FileNotFoundException("Not found directory '" + srcdir.getAbsolutePath() + "'"); if (!trgdir.exists()) { trgdir.mkdir(); System.out.println("Make directory: " + trgdir.getCanonicalPath()); } if (!trgdir.canRead() && !trgdir.canWrite()) { throw new IOException("can not write to '" + srcdir.getAbsolutePath() + "'"); } if (!srcdir.canRead()) { System.err.println(srcdir.getAbsolutePath() + " can't be read."); } convertDir(srcdir, srcCharset, trgdir, trgCharset, filte, includechild); } /** * convertDir,内部方法,转换目录文档 * @param srcdir * @param srcCharset * @param trgdir * @param trgCharset * @param filte * @param include * @throws IOException * @since 1.0.0 2003-12-16 */ protected void convertDir(File srcdir, String srcCharset, File trgdir, String trgCharset, String filte, boolean include) throws IOException { if (!srcdir.exists() || !srcdir.isDirectory()) throw new FileNotFoundException("Not found directory '" + srcdir.getAbsolutePath() + "'"); if (!trgdir.exists()) { trgdir.mkdir(); System.out.println("----------------------"); System.out.println("Make directory: " + trgdir.getCanonicalPath()); } if (!srcdir.canRead()) { System.err.println("----------------------"); System.err.println(srcdir.getAbsolutePath() + " refused access."); } if (!trgdir.canWrite()) { throw new IOException("can not write to '" + srcdir.getAbsolutePath() + "'"); } File[] files = srcdir.listFiles(); String filename = ""; File trgfile = null; for (int i = 0; i < files.length; i++) { filename = files[i].getName(); filename = chtoolkit.convertString(filename, srcCharset, trgCharset); trgfile = new File(trgdir, filename); if (files[i].isFile()) { if (!this.checkPattern(files[i].getName(), filte)) //不符合要求就不做这个 continue; System.out.println("----------------------"); chtoolkit.convertFile(files[i], srcCharset, trgfile, trgCharset); System.out.println("file converted :" + trgfile.getAbsoluteFile()); } else if (include && files[i].isDirectory()) { System.out.println("----------------------"); deep++; System.out.println("directory start " + deep + "#: " + trgfile.getAbsoluteFile()); this.convertDir(files[i], srcCharset, trgfile, trgCharset, filte, include); System.out.println("----------------------"); System.out.println("directory finish " + deep + "#: " + trgfile.getAbsoluteFile()); deep--; } } } private int deep = 0; /** * checkPattern,检查是否符合要求 * @param name * @param pattern * @return boolean true:符合要求 * @since 1.0.0 2003-12-16 */ protected boolean checkPattern(String name, String pattern) { if (pattern == null || pattern.length() == 0) return true; return name.endsWith(pattern); } private static void elapsed() { CharsetToolkit ctk = new CharsetToolkit(); try { ctk.setUseZhCode(true); String gbstring = CharsetToolkit.readInput("AA.txt", "GB2312"); //System.out.println(gbstring); System.out.println("--start--"); int count = 10000; long start = System.currentTimeMillis(); for (int i = 0; i < count; i++) { ctk.convertString(gbstring, "GB2312", "Big5"); } System.out.println(((double) (System.currentTimeMillis() - start)) / count); ctk.setUseZhCode(true); System.out.println(ctk.convertString(gbstring, "GB2312", "Big5")); ctk.setUseZhCode(false); System.out.println(ctk.convertString(gbstring, "GB2312", "Big5")); } catch (Exception e) { e.printStackTrace(); } System.out.println("---end---"); } public static void main(String[] args) { try { new FileConverter().convertDirectory("D:\\srcpath", "GB2312", "D:\\trgpath", "BIG5", ".txt", true); //new FileConverter().convertFile("D:\\srcpath\\AA.txt", "GB2312", "D:\\trg2path\\cc.txt", "BIG5"); } catch (Exception e) { e.printStackTrace(); } System.out.println("---end---"); } }