1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.OutputStream;
import
java.net.InetAddress;
import
java.net.UnknownHostException;
import
java.util.Properties;
import
java.util.Set;
public
class
MyJavaMain {
public
static
void
main(String[] args)
throws
Exception {
//通过StringBuilder来构建要输出的内容
StringBuilder sb =
new
StringBuilder();
sb.append(
"Java 运行时环境版本:"
+System.getProperty(
"java.version"
)+
"\n"
);
sb.append(
"Java 运行时环境供应商:"
+System.getProperty(
"java.vendor"
)+
"\n"
);
sb.append(
"Java 供应商的URL:"
+System.getProperty(
"java.vendor.url"
)+
"\n"
);
sb.append(
"Java 安装目录:"
+System.getProperty(
"java.home"
)+
"\n"
);
sb.append(
"Java 虚拟机规范版本:"
+System.getProperty(
"java.vm.specification.version"
)+
"\n"
);
sb.append(
"Java 类格式版本号:"
+System.getProperty(
"java.class.version"
)+
"\n"
);
sb.append(
"Java类路径:"
+System.getProperty(
"java.class.path"
)+
"\n"
);
sb.append(
"加载库时搜索的路径列表:"
+System.getProperty(
"java.library.path"
)+
"\n"
);
sb.append(
"默认的临时文件路径:"
+System.getProperty(
"java.io.tmpdir"
)+
"\n"
);
sb.append(
"要使用的 JIT 编译器的名称:"
+System.getProperty(
"java.compiler"
)+
"\n"
);
sb.append(
"一个或多个扩展目录的路径:"
+System.getProperty(
"java.ext.dirs"
)+
"\n"
);
sb.append(
"操作系统的名称:"
+System.getProperty(
"os.name"
)+
"\n"
);
sb.append(
"操作系统的架构:"
+System.getProperty(
"os.arch"
)+
"\n"
);
sb.append(
"操作系统的版本:"
+System.getProperty(
"os.version"
)+
"\n"
);
sb.append(
"文件分隔符(在 UNIX 系统中是“/”):"
+System.getProperty(
"file.separator"
)+
"\n"
);
sb.append(
"路径分隔符(在 UNIX 系统中是“:”):"
+System.getProperty(
"path.separator"
)+
"\n"
);
sb.append(
"行分隔符(在 UNIX 系统中是“/n”):"
+System.getProperty(
"line.separator"
)+
"\n"
);
sb.append(
"用户的账户名称:"
+System.getProperty(
"user.name"
)+
"\n"
);
sb.append(
"用户的主目录:"
+System.getProperty(
"user.home"
)+
"\n"
);
sb.append(
"用户的当前工作目录:"
+System.getProperty(
"user.dir"
)+
"\n"
);
//新建一个文件,如果该文件不存在则创建一个
File file =
new
File(
"c:\\t.txt"
);
if
( !file.exists()){
file.createNewFile();
}
OutputStream ous =
new
FileOutputStream(file);
//获取系统的属性
Properties ps = System.getProperties();
//获得系统属性中的键值
Set<String> set = ps.stringPropertyNames();
for
(String name: set){
sb.append(name +
" : "
+ps.getProperty(name) +
"\n"
);
System.out.println(name +
" : "
+ps.getProperty(name) );
}
ous.write(sb.toString().getBytes());
ous.close();
System.out.println(System.getProperty(
"os.name"
));
System.out.println(System.getProperty(
"os.version"
));
System.out.println(
"Java运行时环境版本:"
+System.getProperty(
"java.version"
));
System.out.println(
"Java 运行时环境供应商:"
+System.getProperty(
"java.vendor"
));
System.out.println(
"Java 供应商的URL:"
+System.getProperty(
"java.vendor.url"
));
System.out.println(
"Java 安装目录:"
+System.getProperty(
"java.home"
));
System.out.println(
"Java 虚拟机规范版本:"
+System.getProperty(
"java.vm.specification.version"
));
System.out.println(
"Java 类格式版本号:"
+System.getProperty(
"java.class.version"
));
System.out.println(
"Java 类路径:"
+System.getProperty(
"java.class.path"
));
System.out.println(
"加载库时搜索的路径列表:"
+System.getProperty(
"java.library.path"
));
System.out.println(
"默认的临时文件路径:"
+System.getProperty(
"java.io.tmpdir"
));
System.out.println(
"要使用的 JIT 编译器的名称:"
+System.getProperty(
"java.compiler"
));
System.out.println(
"一个或多个扩展目录的路径:"
+System.getProperty(
"java.ext.dirs"
));
System.out.println(
"操作系统的名称:"
+System.getProperty(
"os.name"
));
System.out.println(
"操作系统的架构:"
+System.getProperty(
"os.arch"
));
System.out.println(
"操作系统的版本:"
+System.getProperty(
"os.version"
));
System.out.println(
"文件分隔符(在 UNIX 系统中是“/”):"
+System.getProperty(
"file.separator"
));
System.out.println(
"路径分隔符(在 UNIX 系统中是“:”):"
+System.getProperty(
"path.separator"
));
System.out.println(
"行分隔符(在 UNIX 系统中是“/n”):"
+System.getProperty(
"line.separator"
));
System.out.println(
"用户的账户名称:"
+System.getProperty(
"user.name"
));
System.out.println(
"用户的主目录:"
+System.getProperty(
"user.home"
));
System.out.println(
"用户的当前工作目录: "
+System.getProperty(
"user.dir"
));
InetAddress netAddress = getInetAddress();
System.out.println(
"host ip:"
+ getHostIp(netAddress));
System.out.println(
"host name:"
+ getHostName(netAddress));
}
/**
* 获取本地主机
* <a href="http://my.oschina.net/u/556800" class="referer" target="_blank">@return</a>
*/
public
static
InetAddress getInetAddress() {
try
{
return
InetAddress.getLocalHost();
}
catch
(UnknownHostException e) {
System.out.println(
"unknown host!"
);
}
return
null
;
}
/**
* 通过InetAddress获取本地Ip
* @param netAddress
* <a href="http://my.oschina.net/u/556800" class="referer" target="_blank">@return</a>
*/
public
static
String getHostIp(InetAddress netAddress) {
if
(
null
== netAddress) {
return
null
;
}
String ip = netAddress.getHostAddress();
return
ip;
}
/**
* 通过InetAddress获取本地主机的名字
* @param netAddress
* <a href="http://my.oschina.net/u/556800" class="referer" target="_blank">@return</a>
*/
public
static
String getHostName(InetAddress netAddress) {
if
(
null
== netAddress) {
return
null
;
}
String name = netAddress.getHostName();
return
name;
}
}
|
然而在Android系统中,却要使用另一种方法才能遍历获得系统的一些属性
1
2
3
4
5
|
Properties ps = System.getProperties();
Set<Entry<Object, Object>> set = ps.entrySet();
for
(Entry<Object, Object> ent : set){
Log.v(
"ent"
,
"key:"
+ ent.getKey() +
" |value: "
+ ent.getValue());
}
|