yegucheng

BlogJava 首页 新随笔 联系 聚合 管理
  9 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

笔者的场景是这样的,笔者使用code smith作为代码生成工具,并在Eclipse中做插件开发,code smith天生
对GB的支持比较弱,只能生成UTF-8编码,这在Eclipse开发的过程中不会存在问题,但是在使用Eclipse的导出
功能时,Eclipse底层使用ANT的执行方式,ANT的默认字符集默认使用当前系统的字符集,这时在编译导出的时候,
会出现字符无法识别的问题,导致导出或者打包失败。
 一种方式可以改变Eclipse工程的默认字符集,以及自动生成的ant配置文件中字符集的配置,这对于单个工程是有
效的,但处理工程间依赖时,被依赖的工程同样会出现字符集问题,即使被依赖工程设定ant的字符集。
 另一种方式,是手工转换,讲UTF-8的字符集转换为GBK的,微软的网站提供了一个批量转换工具,但是在转换之后,
文档的最前面还会有可能存在多于字符,并导致ant打包失败
 最后,没办法自己写了一个字符集转换工具,因为是自己用,所以够用就行,下面是转换部分的代码,实现UTF8到
GBK的转换,其他转换可以对代码稍作修改。

 
import org.apache.commons.lang.ArrayUtils;

public class EncodeRepairTool {
 public static final byte[] bPre = "EFBBBF".getBytes();
 private int i = 0;

 /**
  * @param args
  */
 public static void main(String[] args) {  
  String path = "D:\\eclipse-dev-3.3\\workspace";
  File file = new File(path);
  EncodeRepairTool scanner = new EncodeRepairTool();
  scanner.scanFolder(file);

 }

 

 public void scanFolder(File file) {
  if (file.isDirectory()) {
   File[] files = file.listFiles();
   for (int i = 0; i < files.length; i++) {
    scanFolder(files[i]);
   }
  } else if (file.getName().endsWith(".java")) {
   removePreCode(file);
  }
 }

 private void removePreCode(File file) {
  try {
   FileInputStream fis = new FileInputStream(file);
   int size = fis.available();
   if (size < 24) {
    return;
   }
   i ++ ;
   byte[] bs = new byte[size];
   fis.read(bs);
   byte[] tbs = ArrayUtils.subarray(bs, 0, 3);
   byte[] tbs1 = new byte[] { new Integer(0xEF).byteValue(),
     new Integer(0xBB).byteValue(),
     new Integer(0xBF).byteValue() };
   boolean bol = false;
   if (tbs[0] == tbs1[0] && tbs[1] == tbs1[1] && tbs[2] == tbs1[2]) {
    bol = true;
   }
   fis.close();
   if (!bol) {
    System.out.println("  " + i + " : " + file.getName());
    tbs = bs;
   }
   else {
    System.out.println("**" + i + " : " + file.getName());
    tbs = ArrayUtils.subarray(bs, 3, size);
    
   }   
   InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(tbs), "UTF-8");
   BufferedReader br = new BufferedReader(reader);
   StringBuffer buffer = new StringBuffer();
   String s = br.readLine();
   while (s != null) {
    buffer.append(s);
    buffer.append("\n");
    s =  br.readLine();
   }
   reader.close();
   byte[] nbs = buffer.toString().getBytes("GBK");   
   FileOutputStream fos = new FileOutputStream(file);
   fos.write(nbs);
   fos.flush();
   fos.close();
   
  } catch (FileNotFoundException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  } catch (IOException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }

 }

}

posted on 2007-10-26 10:01 yegucheng 阅读(2214) 评论(5)  编辑  收藏 所属分类: Eclipse插件开发java技术

Feedback

# re: 使用Java API操作文件的字符集 2007-10-26 13:18 bitiwyh
好像使用ant copy可以指定读入/写出出的encoding的.

encoding/outputencoding.
不知道行不行.  回复  更多评论
  

# re: 使用Java API操作文件的字符集 2007-10-26 13:31 yegucheng
可以的,ant的javac也可以指定字符集
但是有三个问题:
1. 通过Eclipse的自动生成工具不会生成encoding以及outEncoding的参数(没有找到修改Eclipse相关模板的地方)
2. 当字符集不正确时,编译的时候会无法解析java 源文件,必须指定javac的运行参数(印象中好像是在javac)
3. Eclipse的插件工程存在依赖关系时,当编译一个插件,依赖到工作区的其他插件时,会同时进行编译,这时候ant中即使制定了参数也不会生效,会抛出字符集错误

  回复  更多评论
  

# re: 使用Java API操作文件的字符集 2007-10-26 13:33 yegucheng
当然如果,是在单个的java工程下,使用ant会简单很多  回复  更多评论
  

# re: 使用Java API操作文件的字符集 2007-10-28 16:06 bitiwyh
哦,我的意思不是说使用ant javac.
javac 是可以指定编码的.

-
我是说,用ant copy
将code smith生成的代码,使用ant copy --> Eclipse工程目录.

  回复  更多评论
  

# re: 使用Java API操作文件的字符集 2007-10-29 09:16 yegucheng
呵呵,确实可以,我开始领会错你的意思了。
我首先是从微软的网站下载的转换工具,结果发现编译还是有问题(文档前端的首字符还是没有去掉),时间紧,就自己写了一个  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: