(一)在web工程中

方法1: 根据系统变量,如获取tomcat物理路径

     String tomcatHome = System.getenv("TOMCAT_HOME");
     String cfg = tomcatHome + File.separator + "webapps"+ File.separator + "bms" + File.separator + "WEB-INF" +     File.separator+"bms_config.properties";

2. 根据request对象

            String tomcatHome = ServletActionContext.getRequest().getRealPath("/");
                     tomcatHome = tomcatHome.substring(0, tomcatHome.indexOf("webapps"));

(二)在普通的java工程中(如做批处理的jar)

    1、利用System.getProperty()函数获取当前路径:
       System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径

     2、使用File提供的函数获取当前路径:
       File directory = new File("");//设定为当前文件夹
        try{
            System.out.println(directory.getCanonicalPath());//获取标准的路径
            System.out.println(directory.getAbsolutePath());//获取绝对路径
            }catch(Exceptin e){}

相关文章:

  • 2022-12-23
  • 2022-02-13
  • 2021-12-25
  • 2021-10-20
  • 2021-05-23
  • 2022-03-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
相关资源
相似解决方案