怎么用native2ascii处理properties ascii文件

下次自动登录
现在的位置:
& 综合 & 正文
用java SDK/bin目录下的native2ascii将ApplicationResources.properties中的中文转化成unicode码实现国际化
1.在clipsee中创建这样一个文件ApplicationResources.properties_zh,自然里边就用来保存中文了.而且系统会自动地以GBK的方式存储该文件,所有该文件在eclipse里也可以显示,不至于像 ApplicationResources.properties 那样出现乱码.
2.用native2ascii工具转换该文件成unicode码,这个工具就在jdk的bin下,使用的方式如下:native2ascii.exe ApplicationResources.properties_zh ApplicationResources.properties
ant:&native2ascii src="${dir.target}" includes="Resources.properties" dest="${dir.target}/ascii" encoding="utf-8"/&
详细说明:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
测试 ResourceBundleTest.java
import java.util.Limport java.util.ResourceBpublic class ResourceBundleTest ...{
public static void main(String args[]) ...{
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", Locale.CHINA);
//LocalStrings 为peoperties文件的文件名称
String title = rb.getString("helloworld.title");
System.out.println(title);
LocalStrings_zh.properties
# This locale is zh_CNhelloworld.title=你好啊~~
输出乱码~~用native2ascii.exe把中文格式化
# This locale is zh_CNhelloworld.title=你好啊~~
输出:你好啊~~在Servlet中的例子应用,Tomcat下的例子,根据request不同选择响应的语言也不相同
/**//** Copyright 2004 The Apache Software Foundation** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**
http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*//**//**//**//* $Id: HelloWorldExample.java,v 1.3
16:40:33 jfarcand Exp $ * */import java.io.*;import java.text.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;/**//** *//**//** * The simplest possible servlet. * * @author James Duncan Davidson */public class HelloWorldExample extends HttpServlet ...{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
ResourceBundle rb =
ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("&html&");
out.println("&head&");
String title = rb.getString("helloworld.title");
out.println("&title&" + title + "&/title&");
out.println("&/head&");
out.println("&body bgcolor="white"&");
// note that all links are created to be relative. this
// ensures that we can move the web application that this
// servlet belongs to to a different place in the url
// tree and not have any harmful side effects.
// making these absolute till we work out the
// addition of a PathInfo issue
out.println("&a href="../helloworld.html"&");
out.println("&img src="../images/code.gif" height=24 " +
"width=24 align=right border=0 alt="view code"&&/a&");
out.println("&a href="../index.html"&");
out.println("&img src="../images/return.gif" height=24 " +
"width=24 align=right border=0 alt="return"&&/a&");
out.println("&h1&" + title + "&/h1&");
out.println("&/body&");
out.println("&/html&");
&&&&推荐文章:
【上篇】【下篇】native2ascii-properties文件转码 - 简书
下载简书移动应用
写了1007字,被4人关注,获得了4个喜欢
native2ascii-properties文件转码
语法:jdk路径\bin&native2ascii -encoding utf8
需要转码到文件
转码后到文件例子:F:\Program Files (x86)\Java\jdk1.6.0_24\bin&\native2ascii -encoding
F:\import\userexcelmodel_cn.properties
F:\import\userexcelmodel_cn1.properties
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
序员教你写代码
· 213人关注
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:Native2Ascii Task
Native2Ascii
Description:
Converts files from native encodings to ASCII with escaped Unicode.
A common usage is to convert source files maintained in a native
operating system encoding, to ASCII prior to compilation.
Files in the directory src
are converted from a native encoding to ASCII.
By default, all files in the directory are converted.
However, conversion may be limited to selected files using
includes and excludes attributes.
For more information on file matching patterns,
see the section on
If no encoding is specified,
the default encoding for the JVM is used.
If ext is specified, then output files are renamed
to use it as a new extension.
More sophisticated file name translations can be achieved using a nested
&mapper& element. By default an
will be used.
If dest and src point to the same directory,
the ext attribute or a nested &mapper&
is required.
This task forms an implicit ,
and supports most attributes of &fileset&
(dir becomes src) as well as
nested &include&, &exclude&,
and &patternset& elements.
It is possible to use different converters. This can be selected
with the implementation attribute or a nested element.
default - the default converter (kaffe or sun) for the platform.
sun (the standard converter of the JDK)
kaffe (the standard converter of )
Description
Reverse the sense of the conversion,
i.e. convert from ASCII to native only supported by the
sun converter
The native encoding the files are in
(default is the default encoding for the JVM)
The directory to find files in (default is basedir)
The directory to output file to
File extension to use in renaming output files
defaultexcludes
indicates whether default excludes should be used or not
(&yes&/&no&).
Default excludes are used when omitted.
comma- or space-separated list of patterns of files that must be
included. All files are included when omitted.
includesfile
the name of a file. Each line of this file is
taken to be an include pattern
comma- or space-separated list of patterns of files that must be excluded.
No files (except default excludes) are excluded when omitted.
excludesfile
the name of a file. Each line of this file is
taken to be an exclude pattern
implementation
The converter implementation to use.
If this attribute is not set, the default converter for the
current VM will be used.
(See the above list of valid converters.)
Parameters specified as nested elements
You can specify additional command line arguments for the converter
with nested &arg& elements.
These elements are
specified like
but have an additional attribute that can be used to enable arguments
only if a given converter implementation will be used.
Description
Exactly one of these.
implementation
Only pass the specified argument if the chosen converter
implementation matches the value of this attribute.
Legal values
are the same as those in the above list of valid compilers.)
implementationclasspath since Apache Ant 1.8.0
holding the
classpath to use when loading the converter implementation if a
custom class has been specified.
Doesn't have any effect when
using one of the built-in converters.
Any nested element of a type that implements Native2AsciiAdapter
since Ant 1.8.0
If a defined type implements the Native2AsciiAdapter
interface a nested element of that type can be used as an
alternative to the implementation attribute.
&native2ascii encoding=&EUCJIS& src=&srcdir& dest=&srcdir&
includes=&**/*.eucjis& ext=&.java&/&
Converts all files in the directory srcdir
ending in .eucjis from the EUCJIS encoding to ASCII
and renames them to end in .java.
&native2ascii encoding=&EUCJIS& src=&native/japanese& dest=&src&
includes=&**/*.java&/&
Converts all the files ending in .java
in the directory native/japanese to ASCII,
placing the results in the directory src.
The names of the files remain the same.
If you want to use a custom
Native2AsciiAdapter org.example.MyAdapter you can either
use the implementation attribute:
&native2ascii encoding="EUCJIS" src="srcdir" dest="srcdir"
includes="**/*.eucjis" ext=".java"
implementation="org.example.MyAdapter"/&
or a define a type and nest this into the task like in:
&componentdef classname="org.example.MyAdapter"
name="myadapter"/&
&native2ascii encoding="EUCJIS" src="srcdir" dest="srcdir"
includes="**/*.eucjis" ext=".java"&
&myadapter/&
&/native2ascii&
in which case your native2ascii adapter can support attributes and
nested elements of its own.leizhimin 的BLOG
用户名:leizhimin
文章数:719
评论数:2665
注册日期:
阅读量:5863
阅读量:12276
阅读量:301720
阅读量:1020896
51CTO推荐博文
JDK自带的native2ascii工具完全揭密
作者:熔岩日期:MSN :
背景:在做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,比如常见的validator验证用的消息资源(properties)文件就需要进行Unicode重新编码。原因是java默认的编码方式为Unicode,而我们的计算机系统编码常常是GBK等编码。需要将系统的编码转换为java正确识别的编码问题就解决了。
1、native2ascii简介:native2ascii是sun java sdk提供的一个工具。用来将别的文本类文件(比如*.txt,*.ini,*.properties,*.java等等)编码转为Unicode编码。为什么要进行转码,原因在于程序的国际化。Unicode编码的定义:Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。1990年开始研发,1994年正式公布。随着计算机工作能力的增强,Unicode也在面世以来的十多年里得到普及。(声明:Unicode编码定义来自互联网)。
2、获取native2ascii:安装了jdk后,假如你是在windows上安装,那么在jdk的安装目录下,会有一个bin目录,其中native2ascii.exe正是。
3、native2ascii的命令行的命名格式:native2ascii -[options] [inputfile [outputfile]]
-[options]:表示命令开关,有两个选项可供选择-reverse:将Unicode编码转为本地或者指定编码,不指定编码情况下,将转为本地编码。-encoding encoding_name:转换为指定编码,encoding_name为编码名称。
[inputfile [outputfile]]inputfile:表示输入文件全名。outputfile:输出文件名。如果缺少此参数,将输出到控制台。
4、最佳实践:首先将JDK的bin目录加入系统变量path。在盘下建立一个test目录,在test目录里建立一个zh.txt文件,文件内容为:“熔岩”,打开“命令行提示符”,并进入C:\test目录下。下面就可以按照说明一步一步来操作,注意观察其中编码的变化。
A:将zh.txt转换为Unicode编码,输出文件到u.txtnative2ascii zh.txt u.txt打开u.txt,内容为“\u”。
B:将zh.txt转换为Unicode编码,输出到控制台
C:\test&native2ascii zh.txt\u可以看到,控制台输出了“\u”。
C:将zh.txt转换为ISO8859-1编码,输出文件到i.txtnative2ascii -encoding ISO8859-1 zh.txt i.txt打开i.txt文件,内容为“\u00c8\u00db\u00d1\u00d2”。
D:将u.txt转换为本地编码,输出到文件u_nv.txtnative2ascii -reverse u.txt u_nv.txt打开u_nv.txt文件,内容为“熔岩”。
E:将u.txt转换为本地编码,输出到控制台C:\test&native2ascii -reverse u.txt熔岩可以看到,控制台输出了“熔岩”。
F:将i.txt转换为本地编码,输出到i_nv.txtnative2ascii -reverse i.txt i_nv.txt打开i_nv.txt文件,内容为“\u00c8\u00db\u00d1\u00d2”。发现转码前后完全一样的。也就是说,等于没有转,或者说思想糊涂,对命名没有理解。。
G:将i.txt转换为GBK编码,输出到i_gbk.txtnative2ascii -reverse -encoding GBK i.txt i_gbk.txt打开i_gbk.txt文件,内容为“\u00c8\u00db\u00d1\u00d2”。发现转码前后完全一样的。也就是说,等于没有转,或者说思想糊涂,对命名没有理解。
H:将u_nv.txt转码到本地编码GBK,输出到控制台C:\test&native2ascii -reverse -encoding ISO8859-1 i.txt熔岩从这个结果看,目标达到到了,编码i.txt为ISO8859-1,转为本地编码后内容为“熔岩”。从这里应该意识到,native2ascii -reverse命令中-encoding指定的编码为源文件的编码格式。而在native2ascii 命令中-encoding指定的编码为(生成的)目标文件的编码格式。这一点非常的重要!切记!!
继续探索,新建文件12a.txt,内容“12axyz”。看看纯字母数字的编码又如何。
I:将纯字母数字的文本文件12a.txt转换为Unicode编码native2ascii 12a.txt 12a_nv.txt打开12a_nv.txt文件,内容为“12axyz”。继续测试,转为ISO8859-1编码看看C:\test&native2ascii -encoding ISOa.txt12axyz结果还是没有转码。从结果可以得出结论:对于纯数字和字母的文本类型件,转码前后的内容是一样的。
5、总结:native2ascii是一个非常的好转码工具,并且转码是可逆的!而其真正的含义并非本地编码――&转码为ASCII码,而是一个通用的文本文件编码转换工具。在做编码转换的时候有两类指定编码的情形,分别指输出文件编码和输入文件编码,具体可以看看最佳实践部分。
声明:原创作品,未经授权,拒绝转载!
了这篇文章
类别:┆阅读(0)┆评论(0)
16:50:33 17:02:03 11:51:21 13:32:53 08:54:36 08:55:50 08:56:10native2ascii的使用方法_百度知道
native2ascii的使用方法
我有更好的答案
\/zhidao/pic/item/4afbfbedab6f4a8cd54.properties(中文字符统一转化为Unicode) 那么使用如下命令JAVA_HOME\bin\native2ascii -encoding GBK D://h.properties D。 假设需要转化的属性文件为.com/zhidao/wh%3D600%2C800/sign=e62012af52fbb2fb347ec9c/4afbfbedab6f4a8cd54:\classes\resources.properties(含有中文字符) :D.jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink"><img class="ikqb_img" src="http.hiphotos.hiphotos:D:\classes\resources.properties<a href="http,转化后的属性文件为:///zhidao/wh%3D450%2C600/sign=d2ec237e9fad66ad125e38/4afbfbedab6f4a8cd54:\src\resources.baidu.hiphotos.baidunative2ascii 工具将带有本机编码字符(非拉丁 1 和非单一码字符)的文件转换成带有Unicode编码字符的文件.jpg" esrc="http://h
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 native2ascii 用法 的文章

 

随机推荐