发表于: java/j2ee | 作者: | 日期: 2013/12/25 10:12
标签:

在sun官方中文文档中,System.getProperty(””)方法中有着这样的翻译:
file.separator
文件分隔符(在 UNIX 系统中是“/”)
path.separator
路径分隔符(在 UNIX 系统中是“:”)
line.separator
行分隔符(在 UNIX 系统中是“/n”)
其实,按照中国人的习惯,以上翻译有些别扭,我觉得应该这样翻译才能更好的被人理解:
file.separator
文件目录(或者文件路径)分隔符(在 UNIX 系统中是“/”,windows中是“\”)
path.separator
环境变量路径分隔符(在 UNIX 系统中是“:”,windows中是“;”)
line.separator
文字换行分隔符(在 UNIX 系统中是“/n”)
至于为什么要说sun官方翻译的有些别扭呢?我给大家举一些例子大家讨论下:
比如我们想要JAVA表示一个下面的文件路径:
windows环境下:
G:\book\api-1.6-cn
linux(unix)环境下:
/home/book/api-1.6-cn
为了跨平台性我们会写出如下的代码:

File[] roots = File.listRoots();// 获取磁盘分区列表
String osname = System.getProperty(“os.name”).toLowerCase();// 返回操作系统名称
// 按sun官方翻译应使用下面的这句,其实得出结果是错的!
// String tempPath=”book”+System.getProperty(“path.separator”)+”api-1.6-cn”;
// 按照我说提出来的翻译,应使用下面的这句,结果正确!
String tempPath = “book” + System.getProperty(“file.separator”)
+ “api-1.6-cn”;
if (osname.indexOf(“windows”) != -1) {// windows下路径表示方法:
for (File file : roots) {
if (file.getPath().indexOf(“G:”) != -1) {
String path = file.getPath() + tempPath;
System.out.println(path);
}
}
} else if (osname.indexOf(“linux”) != -1) {// linux下路径表示方法:
for (File file : roots) {
if (file.getPath().indexOf(“/home”) != -1) {
String path = file.getPath() + tempPath;
System.out.println(path);
}
}
}

[来源:http://hi.baidu.com/ak461230/item/46d3fecca6c1d9d2964452a5]

: https://blog.darkmi.com/2013/12/25/1532.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.