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\
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\
相关文章推荐
- Java字符串String中contains与indexOf的区别
- Set接口
- Java中设置方法执行的超时时间
- HashMap死循环问题分析
- java基础 7种反转方法 charAt()等方法
- java连接mysql与简单操作mysql的增删改查简单例子
- 4. Spring 4.2.3前瞻-使用@Order调整配置类加载顺序
- 关于java实例化学习
- java静态和非静态代码块的执行顺序
- 3. Spring 4.2.3前瞻-@Import注解的升级
- Java异常分类
- 2. Spring 4.2.3前瞻-对java8默认方法(default method)定义Bean的支持
- MyEclipse提示键配置、提示快捷键、提示背景色、关键字颜色、代码显示、编...
- gradle eclipse下构建ssh2项目
- 从头认识java-9.10 Map
- LeetCode第一题Two Sum
- 设计模式之合成模式(Java语言描述)
- JAVA中,comparator接口对数据进行排序
- Java JVM内存结构,区域划分
- 设计模式之合成模式(Java语言描述)