如何获得系统目录啊?各位请帮忙!!!谢谢!!在线等! 给为仁兄,在java里怎么获得系统目录啊?比如我得是windows2000系统,我想得到其系统目录c:\winnt\system32\,怎么弄啊????? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在system.getproperties();里根本得不到,希望各位帮忙啊,在线等啊,一有结果就结帖! import java.io.*;import java.util.*;public class ReadEnv { public static Properties getEnvVars() throws Throwable { Process p = null; Properties envVars = new Properties(); Runtime r = Runtime.getRuntime(); String OS = System.getProperty("os.name").toLowerCase(); // System.out.println(OS); if (OS.indexOf("windows 9") > -1) { p = r.exec( "command.com /c set" ); } else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 20") > -1 ) || (OS.indexOf("windows xp") > -1) ) { // thanks to JuanFran for the xp fix! p = r.exec( "cmd.exe /c set" ); } else { // our last hope, we assume Unix (thanks to H. Ware for the fix) p = r.exec( "env" ); } BufferedReader br = new BufferedReader ( new InputStreamReader( p.getInputStream() ) ); String line; while( (line = br.readLine()) != null ) { int idx = line.indexOf( '=' ); String key = line.substring( 0, idx ); String value = line.substring( idx+1 ); envVars.setProperty( key, value ); // System.out.println( key + " = " + value ); } return envVars; } public static void main(String args[]) { try { Properties p = ReadEnv.getEnvVars(); System.out.println("the current value of TEMP is : " + p.getProperty("TEMP")); } catch (Throwable e) { e.printStackTrace(); } } } 上面的代码给了你一个思路,其实jdk已经不对固定平台的环境变量取得做直接支持,这里我们是通过启动外部进程来实现的。例如,只要你能拿到“windir”系统变量的为“C:\windows”,那么判断一下操作系统的类型(例如XP),那么自然就知道系统目录为“C:\windows\system32”了。简单吧? import java.lang.System;import java.util.Properties;import java.util.Hashtable;import java.util.Enumeration;public class SysVMInfo{ //存放JVM获得的系统属性 static private Properties _property; //Main function public static void main(String[] args) { getSystemProperty(); Hashtable hashKey; hashKey=new Hashtable(); //将系统信息的关键字和标题放到hashtable hashKey.put("java.home", "Java安装目录 "); hashKey.put("java.class.path", "装载类的路径 "); hashKey.put("java.specification.version", "Java API 规范的版本 "); hashKey.put("java.specification.vendor", "Java API 规范的厂商 "); hashKey.put("java.specification.name", "Java API 规范的名称 "); hashKey.put("java.version", "Java API 实现的版本 "); hashKey.put("java.vendor", "Java API 实现的厂商 "); hashKey.put("java.vendor.url", "Java API 规范厂商的URL"); hashKey.put("java.vm.specification.version", "Java虚拟机规范的版本 "); hashKey.put("java.vm.specification.vendor", "Java虚拟机规范的厂商 "); hashKey.put("java.vm.specification.name", "Java虚拟机规范的名称 "); hashKey.put("java.vm.version", "Java虚拟机实现的版本 "); hashKey.put("java.vm.vendor", "Java虚拟机实现的厂商 "); hashKey.put("java.vm.name", "Java虚拟机实现的名称 "); hashKey.put("java.class.version", "Java类文件格式的版本 "); hashKey.put("os.name", "主机操作系统的名称 "); hashKey.put("os.arch", "主机操作系统的体系结构"); hashKey.put("os.version", "主机操作系统的版本 "); hashKey.put("file.separator", "平台目录的分隔符 "); hashKey.put("path.separator", "平台路径的分隔符 "); hashKey.put("line.separator", "平台文本行的分隔符 "); hashKey.put("user.name", "当前用户的帐户名称 "); hashKey.put("user.home", "当前用户的根目录 "); hashKey.put("user.dir", "当前工作目录 "); Enumeration enum; String propertyKey; enum=hashKey.keys(); while(enum.hasMoreElements()) { propertyKey=(String)enum.nextElement(); System.out.println((String)hashKey.get(propertyKey)+":"+_property.getProperty(propertyKey)); } } /** * 获得系统属性列表 * @return Properties */ static public Properties getSystemProperty() { _property = System.getProperties(); return _property; }} 楼上的哥们,你没有理解楼主的真正要求。其实楼主要求的是类似这个效果:C:\>echo %windir%C:\WINDOWS不信你在自己的系统cmd界面里敲一下。呵呵。。 是啊,wingtrace说得对,我是想获得操作系统得安装目录,比如:C:\WINNT\SYSTEM32 那么,我想问一下wingtrace,在windows操作系统中系统目录c:\winnt对应得key是windir,那么在unix以及linux下系统目录对应得key又是什么呢?谢谢回复 呵呵。。楼主啊楼主,所谓“系统目录”本来就是WIN32系统中的概念,Linux和Unix操作系统并不强调这个概念,在Linux/Unix下只有“挂载点”的说法,上面的程序用“env”命令获得的是所有的公开的环境变量,当然你也可以在env后面加上过滤参数,也可以使用echo命令来获得单个的环境变量,这得根据你的实际需要。如果你对Linux/Unix不熟悉,那么等你对WIN32系统和Linux/Unix系统的异同有了初步的了解之后再来考虑这个问题不迟吧。 谢谢wingtrace,看来我的好好学学linux了,好了,本帖子结帖了!还有一个关于xml不能解析文件名伟中文名称的xml文件的问题,我会另外发一个帖子,如果有兴趣可以开看看!我的贴子一般都时在线等,一有结果就结帖!每天都回来这里看看! java如何调用服务器的shell脚本,并判断是否执行成功 在线求一个java小算法 jdbc连oracle的问题 在项目中怎样设计权限,怎样设计最好,什么方法最普遍 欢迎大家加入Java 4 All java数据库中如何按照某个字段的顺序插入记录? 百分求解java.util.zip.Checksum的疑惑,高手们进来讨论一下^_^ 朋友们谁公司要人能帮忙推荐下,万分感谢。2年互联网开发经验,简历我写到内容里了。 ★★★★★★【Applet不支持数据库??!!?】★★★★★★ 条件判断中,字节比较的问题。 请问javadoc如何使用?急。。。在线等 菜鸟问题,求高手解释
import java.util.*;public class ReadEnv {
public static Properties getEnvVars() throws Throwable {
Process p = null;
Properties envVars = new Properties();
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
// System.out.println(OS);
if (OS.indexOf("windows 9") > -1) {
p = r.exec( "command.com /c set" );
}
else if ( (OS.indexOf("nt") > -1)
|| (OS.indexOf("windows 20") > -1 )
|| (OS.indexOf("windows xp") > -1) ) {
// thanks to JuanFran for the xp fix!
p = r.exec( "cmd.exe /c set" );
}
else {
// our last hope, we assume Unix (thanks to H. Ware for the fix)
p = r.exec( "env" );
}
BufferedReader br = new BufferedReader
( new InputStreamReader( p.getInputStream() ) );
String line;
while( (line = br.readLine()) != null ) {
int idx = line.indexOf( '=' );
String key = line.substring( 0, idx );
String value = line.substring( idx+1 );
envVars.setProperty( key, value );
// System.out.println( key + " = " + value );
}
return envVars;
} public static void main(String args[]) {
try {
Properties p = ReadEnv.getEnvVars();
System.out.println("the current value of TEMP is : " +
p.getProperty("TEMP"));
}
catch (Throwable e) {
e.printStackTrace();
}
}
}
import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;public class SysVMInfo
{
//存放JVM获得的系统属性
static private Properties _property; //Main function
public static void main(String[] args)
{
getSystemProperty(); Hashtable hashKey;
hashKey=new Hashtable();
//将系统信息的关键字和标题放到hashtable
hashKey.put("java.home", "Java安装目录 ");
hashKey.put("java.class.path", "装载类的路径 ");
hashKey.put("java.specification.version", "Java API 规范的版本 ");
hashKey.put("java.specification.vendor", "Java API 规范的厂商 ");
hashKey.put("java.specification.name", "Java API 规范的名称 ");
hashKey.put("java.version", "Java API 实现的版本 ");
hashKey.put("java.vendor", "Java API 实现的厂商 ");
hashKey.put("java.vendor.url", "Java API 规范厂商的URL");
hashKey.put("java.vm.specification.version", "Java虚拟机规范的版本 ");
hashKey.put("java.vm.specification.vendor", "Java虚拟机规范的厂商 ");
hashKey.put("java.vm.specification.name", "Java虚拟机规范的名称 ");
hashKey.put("java.vm.version", "Java虚拟机实现的版本 ");
hashKey.put("java.vm.vendor", "Java虚拟机实现的厂商 ");
hashKey.put("java.vm.name", "Java虚拟机实现的名称 ");
hashKey.put("java.class.version", "Java类文件格式的版本 ");
hashKey.put("os.name", "主机操作系统的名称 ");
hashKey.put("os.arch", "主机操作系统的体系结构");
hashKey.put("os.version", "主机操作系统的版本 ");
hashKey.put("file.separator", "平台目录的分隔符 ");
hashKey.put("path.separator", "平台路径的分隔符 ");
hashKey.put("line.separator", "平台文本行的分隔符 ");
hashKey.put("user.name", "当前用户的帐户名称 ");
hashKey.put("user.home", "当前用户的根目录 ");
hashKey.put("user.dir", "当前工作目录 ");
Enumeration enum;
String propertyKey;
enum=hashKey.keys();
while(enum.hasMoreElements())
{
propertyKey=(String)enum.nextElement();
System.out.println((String)hashKey.get(propertyKey)+":"+_property.getProperty(propertyKey));
}
}
/**
* 获得系统属性列表
* @return Properties
*/
static public Properties getSystemProperty()
{
_property = System.getProperties();
return _property;
}
}
C:\WINDOWS不信你在自己的系统cmd界面里敲一下。呵呵。。
谢谢回复