zoukankan      html  css  js  c++  java
  • java路径问题总结

    平时写程序的时候,很多时候提示文件找不到,而抛出了异常,现在整理如下
    【一 相对路径的获得】
    说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目)
    String relativelyPath=System.getProperty("user.dir");
    上述相对路径中,java项目中的文件是相对于项目的根目录
    web项目中的文件路径视不同的web服务器不同而不同(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中的方法类似,不同的是此方法必须以'/'开头)
    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()
    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某一个包中的类,下同)
    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/

    另外,如果想把文件放在某一包中,则可以 通过以下方式获得到文件(先定位到该包的最后一级目录)
    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);
    工程src目录下propertiesTest.properties(名字后缀必须为properties)文件内容如下:
    test=hello word

  • 相关阅读:
    一个小程序的经验总结
    my favorite computer publishers
    关于在天涯看小说
    书店
    Server 2003&Windows 7&Server 2008 R2&Visual Studio&MSDN: my personal best practice
    Google搜索:基本语法
    【我喜欢的一篇文章】Fire And Motion
    Windbg学习笔记(1)
    如何清除Help Viewer 2.0之Filter Contents中的列表内容
    5年了,难道我真的不适合做一个程序员吗,请告诉我我该怎么做?
  • 原文地址:https://www.cnblogs.com/fjhh/p/5370667.html
Copyright © 2011-2022 走看看