详解native2ascii

来源:互联网 发布:福岛核电站事故 知乎 编辑:程序博客网 时间:2024/05/22 14:36

  • 工具的位置
  • 定义
  • 命令
  • 应用场景
  • 最佳实践
  • 未来

工具的位置?

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. 该工具也可以执行逆向转换。可选部分命令:-reversePerform 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_nameSpecifies 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=adminpassword=root

after_converse.properties内容如下:

#\u4e2d\u6587username=adminpassword=root

after_reverse.properties内容如下:

#中文username=adminpassword=root

ISO88591.properties内容如下:

#\u00d6\u00d0\u00ce\u00c4username=adminpassword=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转码可逆。
原创粉丝点击