📄 j-scea-11-3.html
字号:
topnextout.src="../i/next.gif";
bottomnextover.src="../i/h-next.gif";
bottomnextout.src="../i/next.gif";
topnextsectionout.src="../i/nextsection.gif";
topnextsectionover.src="../i/h-nextsection.gif";
bottomnextsectionout.src="../i/nextsection.gif";
bottomnextsectionover.src="../i/h-nextsection.gif";
}
function iOut(image)
{
if (browser=="N3")document[image].src=eval(image + "out.src");
}
function iOver(image)
{
if (browser=="N3")document[image].src=eval(image + "over.src");
}
// -->
</script><a href="#navskip"><img border="0" alt="Skip to main content" src="../i/c.gif"></a>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td class="tbgc" width="160"><a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www.ibm.com/cn"><img height="47" width="160" alt="IBM" border="0" src="../i/ibm-logo.gif"></a></td><td class="tbg" width="195"><font color="#99ccff" size="2"><b>China</b></font><a href="#main"><img alt="Skip to main content" height="1" width="195" border="0" src="../i/c.gif"></a></td><td valign="top" align="right" class="tbgc" width="100%">
<table cellspacing="0" cellpadding="0" border="0">
<form name="form1" id="form1" action="http://210.82.67.149/cgi-bin/dWsearch.pl" method="get">
<tr>
<td colspan="6"><img alt="" height="4" width="390" border="0" src="../i/c.gif"></td>
</tr>
<tr valign="middle">
<td class="dwsearch" height="8"><select class="dwsearch" name="selScope"><option value="dW">dW 中国所有内容</option><option value="dW">................. </option><option value="dW">dW 中国技术专区:</option><option value="comptZ"> 组件技术</option><option value="javaZ"> Java 技术</option><option value="linuxZ"> Linux</option><option value="xmlZ"> XML</option><option value="linuxZ">dW 中国特别专题:</option><option value="securityZ"> 安全</option><option value="unicodeZ"> Unicode</option><option value="all">IBM 全部内容</option></select></td><td class="small"><img alt="" border="0" height="1" width="5" src="../i/c.gif"></td><td><input maxlength="100" size="15" height="15" name="UserRestriction" class="dwsearch" type="text"></td><td><img alt="" height="1" width="5" border="0" src="../i/c.gif"></td><td width="64"><input alt="搜索" name="Search" value="Search" border="0" height="23" width="64" src="../i/search.gif" type="image"></td><td valign="top"><img alt="" height="1" width="34" border="0" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="small" colspan="6"><a target="_blank" style="color: #ffffff;" href="http://210.82.67.149/help.htm">高级搜索</a><img alt="" height="1" width="1" border="0" src="../i/c.gif"></td>
</tr>
<tr>
<td colspan="6"><img alt="" height="4" width="390" border="0" src="../i/c.gif"></td>
</tr>
</form>
</table>
</td>
</tr>
<tr>
<td class="hbg" height="21" width="160"></td><td class="bbg" valign="top" height="21" colspan="2"> <a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/" class="mainlink">IBM 主页</a><span class="divider"> | </span><a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/products/index.shtml" class="mainlink">产品与服务</a><span class="divider"> | </span><a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/support/index.shtml" class="mainlink">支持与下载</a><span class="divider"> | </span><a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/account/index.shtml" class="mainlink">个性化服务</a></td>
</tr>
</table>
<TABLE width="100%" cellspacing="0" cellpadding="0" border="0">
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#cc6633" height="3" width="150"><IMG alt="" src="../i/c.gif" width="150" height="3"></TD>
</TR>
<TR>
<TD bgcolor="#333333" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#000000" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="2"></TD>
</TR>
</TABLE>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><font face="Verdana, sans-serif" size="-1"><a href="/developerWorks/cn/index.shtml">developerWorks 中国网站</a> : <a href="/developerWorks/cn/java/index.shtml">Java</a> : <a href="/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle">教学 - 在线教程</a></font>
<br>
<img alt="Java 认证成功之路,第 4 部分:SCEA" src="imagemaster/masthead.jpg"></td><td width="*" valign="bottom" align="right"><a border="0" href="j-scea.zip"><img alt="下载 ZIP 文件" border="0" src="../i/icon-zip.gif"></a><a target="_blank" href="../tutorial_eng/index.html" border="0"><img alt="英文原文" border="0" src="../i/icon-source.gif"></a>
</td>
</tr>
<tr>
<td height="2" colspan="2"><img alt="" height="2" width="1" src="../i/c.gif"></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index11.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scea-14-1.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img alt="" src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-scea-11-2.html"><img alt="上页" border="0" src="../i/previous.gif" name="topprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scea-11-4.html"><img alt="下页" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="11.国际化" src="imagemaster/titlebar11.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15"> </td><td bgcolor="ffffff" width="12"> </td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>Java 2 国际化功能</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 3 页(共5 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>Java 编程语言有支持国际化/本地化的强大 API。用于此目的的 API 是 Properties、Locale、ResourceBundle、Unicode、java.text package、InputStreamReader 和 OutputStreamWriter。</p>
<p>
<b>Properties</b>
</p>
<p>Properties 类用于在启动时或者运行时从属性文件中装载值。只有字符串对象可以作为键值对存储在属性文件中。可以将属性从流中加载或者保存到流中。一般来说,属性文件包含关于应用程序特性或者其环境的数据。属性对象使用 <code>load()</code> 方法,可以读取本地化的属性文件或者任意输入流来访问相应的本地化的值。</p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
Properties props = new Properties();
String myProps = "MyProperties";
props.load(new BufferedInputStream(new FileInputStream(myProps);
String value = System.getProperty("key");
</code>
</pre>
<p>这段代码展示了如何获取键的值。如果没有键,那么 <code>getProperty()</code> 方法将返回 null。</p>
<p>
<b>Locale</b>
</p>
<p>
<code>java.util.Locale</code> 对象用于识别特定会话或者用户的特定 Locale。一个 Locale 对象是特定区域的标识符。可以根据用户的偏好用任何值设置 Locale 对象。它由一个、两个或者三个元素表示。第一部分包含语言代码,第二部分包含国家代码,第三部分是变量。一个变量允许对一个语言和国家组合使用多个 Locale。Locale 表示为用下划线分隔的语言和国家代码,如 en_US,en_GB。</p>
<p>还有一些不同的常量,如 Locale. US,用于指定不同的 Locale。通过将这些常量作为参数传递,可以用它们创建不同的 Locale 对象。 </p>
<p>
<b>ResourceBundle</b>
</p>
<p>
<code>java.util.ResourceBundle</code> 类用于包含特定于 Locale 的对象或者属性。可以用这个类获取所需要的、特定于 locale 的资源,并很容易使程序本地化或者将它们转换为不同的语言。只需要添加所需要的类或者属性来支持更多的 Locale 即可。 </p>
<p>通过向给 <code>ResourceBundle.getBundle()</code> 方法传递名字和 Locale,可以获取匹配 <code>name_language-code_country-code</code> 的类或者属性文件。如果名字是“Test”,Locale 是“en_US”,那么 ResourceBundle 会扫描名字为 “Test_en_US” 的类或者属性文件。如果没有找到,那么它会继续搜索名为“Test_en" 的文件。如果还没搜索到,则加载默认的 Test.class 或者 Test.Properties 文件。</p>
<p>
<b>Unicode</b>
</p>
<p>Unicode 定义了标准和通用的字符集。JVM 使用 Unicode 字符编码标准。字符和字符串由 16 位字符代码表示。像 java.io package 中 InputStreamReader 和 OutputStreamWriter 这样的类,支持使用不同的编码方案来读取和写入字符数据流。默认编码标准是 ISO 8859-1(ISO Latin_1)。如果应用程序使用默认编码格式无法处理某些字符集,那么应当明确指定编码标准。</p>
<p>Unicode Transformation Format(UTF)是多字节编码格式,它以单字节形式存储一些字符,以双字节或者三字节形式存储另一些字符。如果大多数数据是基于 ASCII 的,那么 UTF 会更紧凑一些,因而它比 Unicode 使用得更广泛。UTF-8 是八位形式的 UTF,是 US-ASCII 与 Unicode 的统一。UTF-8 是一种可变宽度字符编码,它将 16 位 Unicdoe 字符编码为一个或者两个字节。Sun 建议用 UTF-8 形式来编码国际化的内容,因为 UTF-8 与大多数现有的 Web 内容兼容,并提供了对 Unicode 字符集的访问。</p>
<p>
<b>Java.text 包</b>
</p>
<p>java.text 包中有处理文字、日期、数字和消息的类和接口。这些类使用默认 Locale 对象。可以通过指定所选的 Locale 来覆盖这些默认 Locale 行为。可以根据指定的 Locale,用这些类来格式化、解析、搜索和排序受 Locale 影响的信息。 </p>
<p>
<b>InputStreamReader 和 OutputStreamWriter</b>
</p>
<p>InputStreamReader 类以字节的形式从 InputStream 中读取数据,并根据指定的字符编码将它们转换为字符。OutputStreamWriter 类将字符转换为字节,并将它们发送给 OutputStream。JVM 用的是 16 位 Unicode 格式,而许多操作系统使用的是 8 位 Unicode 格式。因此,任何进入 JVM 的数据必须转换为 16 位 Unicode 编码格式,而所有离开 JVM 的数据都必须转换为 8 位 Unicode 编码格式。在创建 Reader 或者 Writer 类时,可以将所需要的编码作为参数传递。ISO 编号表示编码。例如,ISO 8859_1 用 8859_1 传递。如果没有指定任何值,那么就使用平台的默认编码。 </p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index11.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scea-14-1.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img alt="" src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-scea-11-2.html"><img alt="上页" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scea-11-4.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
<script src="//www.ibm.com/common/stats/stats.js" language="JavaScript1.2" type="text/javascript"></script>
<noscript>
<img border="0" alt="" height="1" width="1" src="//stats.www.ibm.com/rc/images/uc.GIF?R=noscript"></noscript>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -