2014-08-29 76 views
0

我在java工作的一个项目,我想知道承认:在java中如何在我的Unix基本系统或Windows上

  1. 如何,如果在我的系统路径使用识别"/""\"
  2. 识别系统的version我运行的应用程序

我将不胜感激代码示例。

+1

如果您正确使用File对象的,你甚至都不需要知道的分隔符。看看所有的构造函数java.io.File必须为你完成这项工作。 – Durandal 2014-08-29 16:17:19

回答

3

这两个通过System Properties

路径分隔符

System.getProperty("path.separator"); 

可用的操作系统版本

System.getProperty("os.version"); 
+1

这就是我正在寻找的。 – 2014-08-29 16:13:47

3
System.getProperties().list(System.out); 
System.out.println(System.getProperty("os.name")); 

System.getProperties()会给你你需要的所有信息。考虑

+0

谢谢我,但我不需要完整的列表。 – 2014-08-29 16:13:27

+0

尝试 System.getProperty(“os.name”),System.getProperty(“os。版本“),System.getProperty(”file.separator“) – 2014-08-29 16:15:14

2
  1. 文件分隔符

    System.getProperty("file.separator") 
    
  2. 操作系统使用:

    System.getProperty("os.name") 
    

此外,让所有的系统正确领带你可以使用:

public static void main(String[] args) { 
    System.getProperties().list(System.out); 
} 
+0

确定前两条语句帮助。 – 2014-08-29 16:14:45

2

这个例子中会列出所有可用的属性

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”)

1
  1. java.io.File.separator;
  2. 系统属性“操作系统名称”:System.getProperty("os.name")
+0

这有助于确定整个事情,但我更具体的问题 – 2014-08-29 16:16:36

0
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 
+0

确定但我更喜欢正则表达式来做诀窍 – 2014-08-29 16:18:01

+1

选择是你的,这是我的风格:) @JeanWilliamEyidi – 2014-08-29 16:19:03

相关问题