问题描述
例如,中国货币的 ISO 4217
代码 CNY
。由于该货币的自由全球交易受到限制,因此存在第二个离岸货币等价物,称为 CNH
。维基百科有一点。
For example, the Chinese currency has the ISO 4217
code CNY
. Since free global trading in that currency is restricted though, there's a second 'offshore' currency equivalent, called CNH
. Wikipedia has a bit of summary of this all.
在 Java 7
中,有一个 JVM发运的三个字母的集合 ISO 4217
代码用。但是,它不能用于向现有国家/地区代码添加单独的货币代码:它将使用 CNH $ c $替换
CNY
c>,这对我的目的没有好处。
In Java 7
, there's a method for updating the set of three letter ISO 4217
codes that the JVM ships with. However, it can't be used to add a separate currency code to an existing country code: it would replace CNY
with CNH
, which is no good for my purposes.
如何添加 CNH
(不在 ISO 4217
list)到 Java 7
中的可用货币集,不会覆盖 CNY
?
How do I add CNH
(which is not in the ISO 4217
list) to the set of available currencies in Java 7
, without overwriting CNY
?
换句话说,如何为一个国家/地区获取多种货币代码?
Put another way, how can I get multiple currency codes for a single country?
请注意这个问题: 询问并回答 Java 6 。但是替换 java.util.CurrencyData
的策略不起作用,因为该文件不再存在。
Note that this question: How do I add the new currency code to Java? was asked and answered for Java 6. But the strategy of replacing java.util.CurrencyData
doesn't work because that file no longer exists.
推荐答案
这里的关键是允许通过替换名为 currency.data
的文件来更新货币列表而无需重建rt.jar。使用这种方法,而不是 currency.properties
覆盖方法,允许您添加新的货币代码,而不会影响来自同一国家/地区的其他货币代码。
The key here is in a change that's part of Java 7 to allow updating of the list of currencies without rebuilding rt.jar by replacing a file called currency.data
. Using this approach, rather than the currency.properties
override approach, allows you to add new Currency codes without affecting other ones from the same country.
还有什么是未说明的,如何实际构建新的 currency.data
。
此文件是从名为 CurrencyData.properties
的文件生成的,可以在java / util的OpenJDK源代码中找到。
What's left unsaid there is how to go about actually building a new currency.data
.This file is generated from a file called CurrencyData.properties
, which can be found in the OpenJDK source code in java/util.
我所做的是复制( openjdk\jdk\src\share\classes\java\util
),并更改了该行:
What I did was copy the CurrencyData.properties
found in the OpenJDK source (openjdk\jdk\src\share\classes\java\util
), and changed the line:
BZD084-CAD124-CDF976-CHF756-CLF990-CLP152-CNY156-COP170-CRC188-CSD891-CUP192-\
到
BZD084-CAD124-CDF976-CHF756-CLF990-CLP152-CNH156-CNY156-COP170-CRC188-CSD891-CUP192-\
然后我在 openjdk \ jdk \的源代码发行版中抓取
。此实用程序以与CurrencyData.properties相同的格式从System.In获取输入,并将其转换为currency.data文件。我稍作改动,以便它使用FileInputStream而不是System.In: GenerateCurrencyData.java
文件make\tools\src\build\tools\generatecurrencydata
Then I grabbed the GenerateCurrencyData.java
file in the source distribution at openjdk\jdk\make\tools\src\build\tools\generatecurrencydata
. This utility takes input from System.In in the same format as CurrencyData.properties, and turns it in to a currency.data file. I made a slight change so that it used a FileInputStream instead of System.In:
currencyData.load(System.in);
到
currencyData.load(new FileInputStream(fileName));
在您编辑的CurrencyData.properties文件中运行该文件,并将原始.data文件放在安全的地方后,将生成的currency.data文件放入JRE \ ltl目录,现在可以运行使用 Currency.getInstance(CNH)
的代码。
Run that on your edited CurrencyData.properties file and, after putting the original .data file somewhere safe, place the resulting currency.data file in to your JRE\lib directory, and you can now run code that uses Currency.getInstance("CNH")
.
这篇关于如何为Java 7中的现有国家/地区代码添加新的Currency到java.util.Currency?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!