我在java
工作的一个项目,我想知道承认:在java中如何在我的Unix基本系统或Windows上
- 如何,如果在我的系统路径使用识别
"/"
或"\"
- 识别系统的
version
我运行的应用程序
我将不胜感激代码示例。
我在java
工作的一个项目,我想知道承认:在java中如何在我的Unix基本系统或Windows上
"/"
或"\"
version
我运行的应用程序我将不胜感激代码示例。
这两个通过System Properties
路径分隔符
System.getProperty("path.separator");
可用的操作系统版本
System.getProperty("os.version");
这就是我正在寻找的。 – 2014-08-29 16:13:47
System.getProperties().list(System.out);
System.out.println(System.getProperty("os.name"));
System.getProperties()会给你你需要的所有信息。考虑
谢谢我,但我不需要完整的列表。 – 2014-08-29 16:13:27
尝试 System.getProperty(“os.name”),System.getProperty(“os。版本“),System.getProperty(”file.separator“) – 2014-08-29 16:15:14
文件分隔符:
System.getProperty("file.separator")
操作系统使用:
System.getProperty("os.name")
此外,让所有的系统正确领带你可以使用:
public static void main(String[] args) {
System.getProperties().list(System.out);
}
确定前两条语句帮助。 – 2014-08-29 16:14:45
这个例子中会列出所有可用的属性
public class SystemDemo {
public static void main(String[] args) {
// this will list the current system properties
Properties p = System.getProperties();
p.list(System.out);
}
为了得到你需要的信息:
操作系统
p.getProperty( “os.name”)
文件分离器
p.get属性(“file.separator”)
java.io.File.separator
;System.getProperty("os.name")
这有助于确定整个事情,但我更具体的问题 – 2014-08-29 16:16:36
System.getProperty("file.separator"); //to get default file separator of O.S
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("win") >=0)
// code for windows
else if(os.indexOf("mac") >= 0)
// code for mac
else if(os.indexOf("nix") >=0 || os.indexOf("nux") >=0)
// code for linux
确定但我更喜欢正则表达式来做诀窍 – 2014-08-29 16:18:01
选择是你的,这是我的风格:) @JeanWilliamEyidi – 2014-08-29 16:19:03
如果您正确使用File对象的,你甚至都不需要知道的分隔符。看看所有的构造函数java.io.File必须为你完成这项工作。 – Durandal 2014-08-29 16:17:19