在软件开发过程中,经常要涉及到多语言支持问题,常用的解决方案是将各个语言文字放到properties文件中,但中文是需要转为ASCII的 。那么如何将中文进行转换呢,下面就为你列举几种比较方便的方法 
一.jdk的native2ascii
举例:
1.转换一行中文文字
进入jdk的bin目录,双击native2ascii.exe,输入“你好”,【回车】,屏幕上显示/u4f60/u597d
2.转换一个文件
cmd命令行中执行(注意配置好path环境变量) 
d:/>native2acsii aa.properties bb.properties 
如此一来就aa.properties 中的中文字符转换后保存进bb.properties 文件中。注意如果其中含有英文字符,则原样输出。
 
二.ant
    可以一次就转换n多个文件
  
           
  

三.eclipse插件

如果是MyEclipse,安装插件方式如下:
安装步骤:
1、展开Eclipse的Help菜单,将鼠标移到Software Update子项,在出现的子菜单中点击Find and Install; 
2、在Install/Update对话框中选择Search for new features to install,点击Next; 
3、在Install对话框中点击New Remote Site; 
4、在New Update Site对话框的Name填入任意字符串中文也可以,在URL中填入http://propedit.sourceforge.jp/eclipse/updates/;然后可能需要先点击finish,不过依然会出现后面的部分(根据版本的不同而定) 
5、在Site to include to search列表中,除上一步加入的site外的其它选项去掉,点击Finsih; 
6、在弹出的Updates对话框中的Select the features to install列表中将所有结尾为“3.1.x”的选项去掉(适用于Eclipse 3.2版本的朋友); 
7、点击Finish关闭对话框; 
8、在下载后,同意安装,再按提示重启Eclipse,在工具条看到形似vi的按钮表示安装成功,插件可用。此时,Eclpise中所有properties文件的文件名前有绿色的P的图标作为标识。
9、properties文件使用PropertiesEditor(右键,openwith,一般来说安装插件后默认就会使用这个编辑器)打开,输入中文,编辑器会自动将其转换为ascii码
 
如果是Eclipse,展开help,选择Install new software,add,输入地址,确定后eclipse会进行自动搜索,不过安装插件时会出现无法安装的情况,此时可以下载我提供的安装包,
下载地址 :http://download.csdn.net/source/1488797
 
可以直接将插件包解压后的features和plugins两个文件夹复制到eclipse的home目录下,重启eclipse即可。
 
  四、编写转换ASCII的java程序,并执行
public class Ascii2Native {
public static void main(String[] args) { File f=new
File("E://workspaces//cas_workspace//CASServer//src//messages_zh_CN.properties");
if (f.exists() && f.isFile()) {
// convert param-file
BufferedReader br = null;
String line;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "JISAutoDetect"));
while ((line = br.readLine()) != null) {
System.out.println(ascii2native(line));
}
} catch (FileNotFoundException e) {
System.err.println("file not found - " + f);
} catch (IOException e) {
System.err.println("read error - " + f);
} finally {
try {
if (br != null)
br.close();
} catch (Exception e) {
}
}
} else {
// // convert param-data
// System.out.print(ascii2native(args[i]));
// if (i + 1 < args.length)
System.out.print("wen");
}
} public static String ascii2native(String str) {
String hex = "0123456789ABCDEF";
StringBuffer buf = new StringBuffer();
int ptn = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '//' && i + 1 <= str.length() && str.charAt(i + 1) == '//') {
buf.append("////");
i += 1;
} else if (c == '//' && i + 6 <= str.length() && str.charAt(i + 1) == 'u') {
String sub = str.substring(i + 2, i + 6).toUpperCase();
int i0 = hex.indexOf(sub.charAt(0));
int i1 = hex.indexOf(sub.charAt(1));
int i2 = hex.indexOf(sub.charAt(2));
int i3 = hex.indexOf(sub.charAt(3));
if (i0 < 0 || i1 < 0 || i2 < 0 || i3 < 0) {
buf.append("//u");
i += 1;
} else {
byte[] data = new byte[2];
data[0] = i2b(i1 + i0 * 16);
data[1] = i2b(i3 + i2 * 16);
try {
buf.append(new String(data, "UTF-16BE").toString());
} catch (Exception ex) {
buf.append("//u" + sub);
}
i += 5;
}
} else {
buf.append(c);
}
}
return buf.toString();
} private static byte i2b(int i) {
return (byte) ((i > 127) ? i - 256 : i);
}
}

ps:最近发现了一个更好用的插件,可以同时编辑一组properties文件,http://www.guh-software.de/jinto_en.html,恩好东西。


搬迁源地址http://blog.sina.com.cn/s/blog_7671643b01015qnm.html

properties 文件的中文转ASCII的更多相关文章

  1. properties文件中中文不能显示或者中文乱码

    1.properties 文件中文乱码问题 鼠标“右击”文件 => Resource => Text file encoding => UTF-8 2.properties 文件解析 ...

  2. springBoot使用@Value标签读取*.properties文件的中文乱码问题

    上次我碰到获取properties文件中的中文出现乱码问题. 查了下资料,原来properties默认的字符编码格式为asci码,所以我们要对字符编码进行转换成UTF-8格式 原先代码:@Proper ...

  3. Eclipse中properties文件,中文只显示Unicode问题(Properties Editor)

    我们常常在properties文件中添加中文注释,而properties文件的中文需用unicode表示, 使用eclipse默认的properties文件编辑器查看显示中文为乱码. 即便修改prop ...

  4. 读取Properties文件以及中文乱码问题

    在java类中常见的读取Properties文件方式,是使用Properties.load(inputStream);的方式但是常常出现中文乱码问题,这就很尴尬了 public synchronize ...

  5. Eclipse中 properties 文件中 中文乱码

    在.properties文件写注释时,发现中文乱码了,由于之前在idea中有见设置.properties文件的编码类型,便找了找乱码原因 在中文操作系统中,Eclipse中的Java类型文件的编码的默 ...

  6. spring使用@Value标签读取.properties文件的中文乱码问题的解决

    最近测试某个老系统的时候,启动的时候发@Value注入的中文是乱码,文件使用GBK/UTF-8的时候均会出现乱码问题,但是spring配置文件里面注入的占位符并没有这个问题,bean文件设置了file ...

  7. ResourceBundle (读取properties文件及中文乱码解决方法)

    原文:http://blog.csdn.net/joecheungdishuiya/article/details/6304993 public class test { static Resourc ...

  8. IntelliJ IDEA设置properties文件显示中文

    配置这里: 注意:上面是Default Settings,还需要在Settings中设置成上面一样的.

  9. 设置IDEA中properties文件显示中文

    路径: File - Setting - Editor - Code Style - File Encodings

随机推荐

  1. MySQL Cluster-备份恢复初步测试

    参考文档   http://blog.chinaunix.net/uid-20639775-id-1617795.html  http://xxtianxiaxing.iteye.com/blog/5 ...

  2. setuptools,easy_install使用

    当需要安装第三方python包时,可能会用到easy_install命令.easy_install是由PEAK(Python Enterprise Application Kit)开发的setupto ...

  3. 使用msys

    下载地址:http://msys2.github.io/ 更新:pacman -Syu 安装git:pacman -S git 或者使用cygwin 调色:编辑~/.minttyrc Foregrou ...

  4. 1298 The Hardest Problem Ever

    题目链接:http://poj.org/problem?id=1298 思路分析:水题,字符偏移求解,注意字符串输入问题即可. 代码如下: #include <iostream> #inc ...

  5. Save Princess(丑数)

    Save Princess 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Yesterday, the princess was kidnapped by a de ...

  6. PHP - __clone 对象克隆

    <?php /** * 此例子解释什么是深克隆. * 克隆学生类. */ class Student { public $name = '张三'; public $age = 12; //所属老 ...

  7. PHP学习笔记3-逻辑运算符

    逻辑运算符图解: 逻辑且&&: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/26 ...

  8. Ubuntu网络频繁掉线解决方案

    年底了,实验室终于给配了个电脑(Ubuntu系统),博主欣喜若狂啊,然而装好后发现无线网频繁掉线,重启网络后能正常上网2~3分钟然后又掉线,再重启又能上网2~3分钟然后再掉线,博主那个不爽啊,于是各种 ...

  9. smarty 截取字符串,调用php中的方法,foreach循环

    1.smarty截取字符串       html中的代码    <{$content|truncate:30:"..."}>                       ...

  10. 基于visual Studio2013解决算法导论之029二叉搜索树

     题目 二叉搜索树 解决代码及点评 #include <stdio.h> #include <malloc.h> #include <stdlib.h> ty ...