There is such a method getProperties() in the System class in the JDK documentation. The following parameters are available for use in the detailed introduction of this method:
java.version
Java runtime environment version
java.vendor
Java runtime environment vendor
java.vendor.url
Java vendor URL
java.home
Java installation directory
java.vm.specification.version
Java Virtual Machine Specification Version
java.vm.specification.vendor
Java Virtual Machine Specification Vendor
java.vm.specification.name
Java Virtual Machine specification name
java.vm.version
Java Virtual Machine implementation version
java.vm.vendor
Java Virtual Machine Implementation Vendor
java.vm.name
Java virtual machine implementation name
java.specification.version
Java Runtime Environment specification version
java.specification.vendor
Java Runtime Environment Specification Vendor
java.specification.name
Java Runtime Environment specification name
java.class.version
Java class format version number
java.class.path
Java class path
java.library.path
A list of paths searched when loading libraries
java.io.tmpdir
default temporary file path
java.compiler
The name of the JIT compiler to use
java.ext.dirs
Path to one or more extension directories
os.name
the name of the operating system
os.arch
operating system architecture
os.version
The version of the operating system
file.separator
file separator ("/" on UNIX systems)
path.separator
path separator (":" on UNIX systems)
line.separator
行分隔符(在 UNIX 系统中是“/n”)
user.name
用户的账户名称
user.home
用户的主目录
user.dir
用户的当前工作目录
下面是针对这些参数进行的一个测试,代码如下:
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));
}
/**
* 获取本地主机
* @return
*/
public static InetAddress getInetAddress() {
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
System.out.println("unknown host!");
}
return null;
}
/**
* 通过InetAddress获取本地Ip
* @param netAddress
* @return
*/
public static String getHostIp(InetAddress netAddress) {
if (null == netAddress) {
return null;
}
String ip = netAddress.getHostAddress();
return ip;
}
/**
* 通过InetAddress获取本地主机的名字
* @param netAddress
* @return
*/
public static String getHostName(InetAddress netAddress) {
if (null == netAddress) {
return null;
}
String name = netAddress.getHostName();
return name;
}
}
然而在Android系统中,却要使用另一种方法才能遍历获得系统的一些属性
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());
}