详解native2ascii

工具的位置?

D:\Program Files\Java\jdk1.8.0_102\bin\native2ascii.exe

即jdkbin目录下有该工具。

定义

Creates localizable applications  创建本地化应用程序
by converting a file with characters in any supported character encoding 通过将某种字符编码的文件转化成任意支持的字符编码的文件
to one with ASCII and/or Unicode escapes ASCII编码的文件或Unicode转义的文件
or vice versa. 反之亦然

通过将一个以某种方式编码的【文件】转成其他任意支持编码的文件
创建本地化应用程序

命令

native2ascii [ inputfile ] [ outputfile ]

inputfile : The encoded file to be converted to ASCII.

outputfile :The converted ASCII file.

描述:
1. 在java运行环境(jre)的支持下,命令native2ascii把某种字符编码的文件转化成ASCII字符编码的文件,即把不是ASCII字符编码用Unicode转义(\uxxxx)区分。
2. 这种操作对包含不是ISO-88591-1字符的文件来说是必须的。
3. 该工具也可以执行逆向转换。


可选部分命令:

-reverse
Perform the reverse operation: Converts a file encoded in ISO-8859-1 with Unicode escapes to a file in any character encoding supported by the JRE.
执行逆向操作:将ISO-8859-1的文件转化成JRE支持编码的文件。

-encoding encoding_name
Specifies the name of the character encoding to be used by the conversion procedure. If this option is not present, then the default character encoding (as determined by the java.nio.charset.Charset.defaultCharset method) is used. The encoding_name string must be the name of a character encoding that is supported by the JRE. 

指定转换的字符编码名称,如果不指定,则使用默认的。要注意的是编码名称必须是JRE所支持的,如下所示
http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

应用场景

.properties文件默认编码格式是ISO-8859-1即我们所熟知的Latin-1。所有非该编码的字符必须用Unicode转义字符转义掉。
否则无法辨认

最佳实践

before_converse.properties内容如下:

#中文
username=admin
password=root

after_converse.properties内容如下:

#\u4e2d\u6587
username=admin
password=root

after_reverse.properties内容如下:

#中文
username=admin
password=root

ISO88591.properties内容如下:

#\u00d6\u00d0\u00ce\u00c4
username=admin
password=root

//前置条件
C:\Users\Administrator>D:
D:\>cd D:\Program Files\Java\jdk1.8.0_102\bin

######################默认编码、解码######################
//操作一:不指定编码(使用默认编码)
D:\Program Files\Java\jdk1.8.0_102\bin>native2ascii D:\before_converse.properties D:\after_converse.properties

//操作二:还原文件
D:\Program Files\Java\jdk1.8.0_102\bin>native2ascii -reverse D:\after_converse.propertiesD:\after_reverse.properties

######################不将结果输出到文件,而是直接输出到控制台######################
//操作三:不指定编码(使用默认编码)
D:\Program Files\Java\jdk1.8.0_102\bin>native2ascii D:\before_converse.properties

//操作四:不指定编码(使用默认编码)
D:\Program Files\Java\jdk1.8.0_102\bin>native2ascii -reverse D:\after_converse.properties

######################使用-encoding参数######################
//ISO-8859-1编码
D:\Program Files\Java\jdk1.8.0_102\bin>native2ascii -encoding ISO-8859-1 D:\before_converse.properties D:\ISO88591.properties

//ISO-8859-1解码,输出内容和after_reverse.properties一样
D:\Program Files\Java\jdk1.8.0_102\bin>native2ascii -encoding ISO-8859-1 -reverse D:\ISO88591.properties

未来

JDK9的到来,.properties编码默认的将是UTF-8,对于非UTF-8字符的将转成ISO-8859-1.点我见详情

  • 备注:
    1. 对于纯数字和字母的文本类型件,转码前后的内容是一样的
    2. Native2ascii转码可逆。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值