您的位置:首页 > 编程语言 > Java开发

java取得当前工作目录

2015-11-19 22:56 387 查看
public static void main(String[] args){ 

   System.out.println("Java运行时环境版本:\n"+System.getProperty("java.version")); 

   System.out.println("Java 运行时环境供应商:\n"+System.getProperty("java.vendor")); 

   System.out.println("Java 供应商的URL:\n"+System.getProperty("java.vendor.url")); 

   System.out.println("Java安装目录:\n"+System.getProperty("java.home")); 

   System.out.println("Java 虚拟机规范版本:\n"+System.getProperty("java.vm.specification.version")); 

   System.out.println("Java 类格式版本号:\n"+System.getProperty("java.class.version")); 

   System.out.println("Java类路径:\n"+System.getProperty("java.class.path")); 

   System.out.println("加载库时搜索的路径列表:\n"+System.getProperty("java.library.path")); 

   System.out.println("默认的临时文件路径:\n"+System.getProperty("java.io.tmpdir")); 

   System.out.println("要使用的 JIT 编译器的名称:\n"+System.getProperty("java.compiler")); 

   System.out.println("一个或多个扩展目录的路径:\n"+System.getProperty("java.ext.dirs")); 

   System.out.println("操作系统的名称:\n"+System.getProperty("os.name")); 

   System.out.println("操作系统的架构:\n"+System.getProperty("os.arch")); 

   System.out.println("操作系统的版本:\n"+System.getProperty("os.version")); 

   System.out.println("文件分隔符(在 UNIX 系统中是“/”):\n"+System.getProperty("file.separator")); 

   System.out.println("路径分隔符(在 UNIX 系统中是“:”):\n"+System.getProperty("path.separator")); 

   System.out.println("行分隔符(在 UNIX 系统中是“/n”):\n"+System.getProperty("line.separator")); 

   System.out.println("用户的账户名称:\n"+System.getProperty("user.name")); 

   System.out.println("用户的主目录:\n"+System.getProperty("user.home")); 

 

 

在写java程序时不可避免要获取文件的路径...总结一下,遗漏的随时补上

1.可以在servlet的init方法里

String path = getServletContext().getRealPath("/");

这将获取web项目的全路径

例如 :E:\eclipseM9\workspace\tree\

tree是我web项目的根目录

2.你也可以随时在任意的class里调用

this.getClass().getClassLoader().getResource("/").getPath();

这将获取 到classes目录的全路径

例如 : E:\eclipseM9/workspace/tree/WEB-INF/classes/

这个方法也可以不在web环境里确定路径,比较好用

3.request.getContextPath();

获得web根的上下文环境

如 /tree

tree是我的web项目的root context

/*jsp 取得当前目录的路径

path=request.getRealPath("");

/*得到jbossWEB发布临时目录 warUrl=.../tmp/deploy/tmp14544test-exp.war/

path=C:\jboss-4.0.5.GA\server\default\tmp\deploy\tmp14544test-exp.war\

String   path   =   (String)request.getContextPath();

/*得到项目(test)应用所在的真实的路径 path=/test 

String     path     =   request.getRequestURI();

/*得到应用所在的真实的路径 path=/test/admin/admindex.jsp

String   savePath=request.getRealPath(request.getServletPath());

/*得到当前文件的磁盘绝对路径  

//JAVA 取得当前目录的路径

File   file=new   File(".");   

String path=file.getAbsolutePath();

                path=file.getPath();

/*得到jboss运行目录 path=C:\jboss-4.0.5.GA\bin\
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: