zoukankan      html  css  js  c++  java
  • Java获取项目路径下的方法(全)

    平时敲代码的时候,非常多时候提示文件找不到,而抛出了异常,如今整理例如以下

    一 相对路径的获得
    说明:相对路径(即不写明时候究竟相对谁)均可通过下面方式获得(不论是一般的java项目还是web项目)
    String relativelyPath=System.getProperty("user.dir");
    上述相对路径中,java项目中的文件是相对于项目的根文件夹
    web项目中的文件路径视不同的webserver不同而不同(tomcat是相对于 tomcat安装文件夹in)


    二 类载入文件夹的获得(即当执行时某一类时获得其装载文件夹)
    1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级文件夹)


    InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
    (test.txt文件的路径为 项目名src est.txt;类TestAction所在包的第一级文件夹位于src文件夹下)


    上式中将TestAction。test.txt替换成相应成相应的类名和文件名称字就可以


    1.2)通用方法二 (此方法和1.1中的方法类似,不同的是此方法必须以'/'开头,參考http://riddickbryant.javaeye.com/blog/436693)
    InputStream is=Test1.class.getResourceAsStream("/test.txt");
    (test.txt文件的路径为 项目名src est.txt,类Test1所在包的第一级文件夹位于src文件夹下)


    三 web项目根文件夹的获得(公布之后)
    1 从servlet出发

    可建立一个servlet在其的init方法中写入例如以下语句
    ServletContext s1=this.getServletContext();
    String temp=s1.getRealPath("/"); (关键)
    结果形如:D:工具Tomcat-6.0webapps02_ext (002_ext为项目名字)


    假设是调用了s1.getRealPath("")则输出D:工具Tomcat-6.0webapps02_ext(少了一个"")


    2 从httpServletRequest出发


    String cp11111=request.getSession().getServletContext().getRealPath("/");


    结果形如:D:工具Tomcat-6.0webapps02_ext


    四 classpath的获取(在Eclipse中为获得src或者classes文件夹的路径)

    方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()


    eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
    System.out.println("t---"+t);


    输出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/


    方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse为src某一个包中的类,下同)


    eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
    System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);


    输出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/


     


    另外,假设想把文件放在某一包中,则能够 通过下面方式获得到文件(先定位到该包的最后一级文件夹)


    eg String p2=JdomParse.class.getResource("").getPath();
    System.out.println("JdomParse.class.getResource---"+p2);


    输出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse为src文件夹下jdom包中的类)


    四 属性文件的读取:


    方法 一


    InputStream in = lnew BufferedInputStream( new FileInputStream(name));    Properties p = new Properties();   p.load(in);



    注意路径的问题,做运行之后就能够调用p.getProperty("name")得到相应属性的值

    方法二

    Locale locale = Locale.getDefault();
    ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale);
    String value = localResource.getString("test");
    System.out.println("ResourceBundle: " + value);

    projectsrc文件夹下propertiesTest.properties(名字后缀必须为properties)文件内容例如以下:

    test=hello word


    欢迎增加qq交流群:398918539

  • 相关阅读:
    接口方法上的注解无法被@Aspect声明的切面拦截的原因分析
    SpringBoot整合Netty
    简单的RPC框架
    基于redis的分布式锁的分析与实践
    8种方案解决重复提交问题
    领券中心项目,如何用 Redis 做实时订阅推送的?
    IM(即时通讯)服务端(二)
    IM(即时通讯)服务端(一)
    0xC00000FD: Stack overflow (parameters: 0x00000000, 0x003E2000).错误
    int (*a)[10]和int *a[10]的区别
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7044935.html
Copyright © 2011-2022 走看看