zoukankan      html  css  js  c++  java
  • java获取路径(转)

    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){}

    File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。

    # 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
    # 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径
    # 至于getPath()函数,得到的只是你在new File()时设定的路径

    比如当前的路径为 C: est :
    File directory = new File("abc");
    directory.getCanonicalPath(); //得到的是C: estabc
    directory.getAbsolutePath();    //得到的是C: estabc
    direcotry.getPath();                    //得到的是abc

    File directory = new File(".");
    directory.getCanonicalPath(); //得到的是C: est
    directory.getAbsolutePath();    //得到的是C: est.
    direcotry.getPath();                    //得到的是.

    File directory = new File("..");
    directory.getCanonicalPath(); //得到的是C:
    directory.getAbsolutePath();    //得到的是C: est..
    direcotry.getPath();                    //得到的是..


    另外:System.getProperty()中的字符串参数如下:

    System.getProperty()参数大全
    # 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                 List of paths to search when loading libraries 
    # java.io.tmpdir                       Default temp file path 
    # java.compiler                       Name of JIT compiler to use 
    # java.ext.dirs                       Path of extension directory or directories 
    # os.name                              Operating system name 
    # os.arch                                  Operating system architecture 
    # os.version                       Operating system version 
    # file.separator                         File separator ("/" on UNIX) 
    # path.separator                  Path separator (":" on UNIX) 
    # line.separator                       Line separator (" " on UNIX) 
    # user.name                        User's account name 
    # user.home                              User's home directory 
    # user.dir                               User's current working directory

     
     
    3.如何获得当前类文件路径
    常用:
    (1).Test.class.getResource("")
    得到的是当前类FileTest.class文件的URI目录。不包括自己!
    (2).Test.class.getResource("/")
    得到的是当前的classpath的绝对URI路径。
    (3).Thread.currentThread().getContextClassLoader().getResource("")
    得到的也是当前ClassPath的绝对URI路径。
    (4).Test.class.getClassLoader().getResource("")
    得到的也是当前ClassPath的绝对URI路径。
    (5).ClassLoader.getSystemResource("")
    得到的也是当前ClassPath的绝对URI路径。
    尽量不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径,后面可以看出得出结果五花八门。
    (6) new File("").getAbsolutePath()也可用。
           
    4.Web服务器
    (1).Tomcat
    在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
    (2).Resin
    不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET
    的路径为根.比如用新建文件法测试File f = new File("a.htm");
    这个a.htm在resin的安装目录下 
    (3).如何读文件
    使用ServletContext.getResourceAsStream()就可以
    (4).获得文件真实路径
    String   file_real_path=ServletContext.getRealPath("mypath/filename");  
    不建议使用request.getRealPath("/"); 
  • 相关阅读:
    nginx添加location跳转后不生效
    UniApp微信小程序授权获取用户当前位置信息
    VS创建Core项目体验跨平台,部署在docker上运行(启用docker支持)
    在Unity中渲染一个黑洞
    一个简简单单的红点系统框架
    十一、Abp vNext 基础篇丨测试
    Abp vNext 番外篇-疑难杂症丨浅谈扩展属性与多用户设计
    十、Abp vNext 基础篇丨权限
    九、Abp vNext 基础篇丨评论聚合功能
    Abp vNext 番外篇-疑难杂症丨nginx反向代理-部署
  • 原文地址:https://www.cnblogs.com/azhqiang/p/3851835.html
Copyright © 2011-2022 走看看