2012-01-04 26 views

回答

11
boolean success = new java.io.File(System.getProperty("user.home"), "directory_name").mkdirs(); 
+0

'File'中沒有'mkdirs' – 2012-01-04 00:42:22

+3

再看一遍。 http://docs.oracle.com/javase/1.5.0/docs/api/java/io/File.html#mkdirs() – Synesso 2012-01-04 00:43:46

+1

我的不好.-------- – 2012-01-04 01:05:57

1

System Properties

改善後的答案!收集所有信息並將其放在一起。

public static void main(String[] args) { 
    String myDirectory = "Yash"; // user Folder Name 
    String path = getUsersHomeDir() + File.separator + myDirectory ; 

    if (new File(path).mkdir()) { 
     System.out.println("Directory is created!"); 
    }else{ 
     System.out.println("Failed to create directory!"); 
    } 
    getOSInfo(); 
} 
public static void getOSInfo(){ 
    String os = System.getProperty("os.name"); 
    String osbitVersion = System.getProperty("os.arch"); 
    String jvmbitVersion = System.getProperty("sun.arch.data.model"); 

    System.out.println(os + " : "+osbitVersion+" : "+jvmbitVersion); 
} 
public static String getUsersHomeDir() { 
    String users_home = System.getProperty("user.home"); 
    return users_home.replace("\\", "/"); // to support all platforms. 
} 

要打印所有可用的屬性。

for (Entry<Object, Object> e : System.getProperties().entrySet()) { 
    System.out.println(String.format("%s = %s", e.getKey(), e.getValue())); 
} 
相關問題