Explore. Dream. Discover.

Samuel Chen's life

Twenty years from now you will be more disappointed by
the things that you didn't do than by the ones you did do.
So throw off the bowlines. Sail away from the safe harbor.
Catch the trade winds in your sails.

Explore. Dream. Discover.
—— Mark Twain

2004年9月15日 星期三

[摘录]JAVA如何使用相对路径

---摘自互联网

我们在程序中经常要用到数据文件或资源文件(如图象),如果在程序中用绝对路径,就会使程序失去灵活性。例如我们的应用程序目录是D:\NECS,而数据目录是 D:\NECS\DataBase,如果我们使用数据文件时用D:\NECS\DataBase目录名。那么当我们程序被移到C盘的时候,就会出错,因为这时数据目录变成了C:\NECS\DataBase。所以我们要使用相对目录 \DataBase。

我们可以使用类java.util.Properties ,该类是一个Hashtable,保存许多系统属性。其中:

……
java.home Java installation directory
java.class.path Java class path
file.separator File separator ("/" on UNIX)
user.dir User's current working directory
……


我们可以用java.lang.System的方法getProperty(String key)来获得所需的属性。

curDir = System.getProperty("user.dir");
fileBar= System.getProperty("file.separator");

则上例中的数据目录,可表示为 curDir+fileBar+"DataBase"

标签: ,

2004年9月1日 星期三

[JSTL] 使用本地化资源文件的方法

以basename是messages的为例。

内容如下的utf-8的中文化文件messages_zh_CN.properties.src:

name=张三
sex=男
city=北京
menu_return=返回主菜单

第一步,先确定原文件是GB/GBK编码的(很重要),如果是utf-8的先要转成GB/GBK的;

第二步,用$JDK/bin/native2ascii.exe进行转换
c:\path>native2ascii messages_zh_CN.properties.src >
messages_zh_CN.properties

实际产生的输出如下:

name=\u5f20\u4e09
sex=\u7537
city=\u5317\u4eac
menu_return=\u8fd4\u56de\u4e3b\u83dc\u5355


由于重定向到文件,所以看不到输出。

以上内容就是你的本地化的资源文件messages_zh_CN.properties的内容。

注意:.properties文件末尾需要一个空行才能被native2ascii正确转换。

标签: ,