zoukankan      html  css  js  c++  java
  • File文件以及.propertites文件操作

    • File文件操作
      1. 在jsp和class文件中调用的相对路径不同。在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。( 不过听说在linux下无法获取,未试验)

      2. 另:在Jsp,Servlet,Java中详细获得路径的方法!

      3. 1.jsp中取得路径:

      4. 以工程名为TEST为例:

      5. (1)得到包含工程名的当前页面全路径:request.getRequestURI()

      6. 结果:/TEST/test.jsp

      7. (2)得到工程名:request.getContextPath()

      8. 结果:/TEST

      9. (3)得到当前页面所在目录下全名称:request.getServletPath()

      10. 结果:如果页面在jsp目录下 /TEST/jsp/test.jsp

      11. (4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")

      12. 结果:D: esinwebappsTEST est.jsp

      13. (5)得到页面所在服务器的绝对路径:absPath=newjava.io.File(application.getRealPath(request.getRequestURI())).getParent();

      14. 结果:D: esinwebappsTEST

      15. 2.在类中取得路径:

      16. (1)类的绝对路径:Class.class.getClass().getResource("/").getPath()

      17. 结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/

      18. (2)得到工程的路径:System.getProperty("user.dir")

      19. 结果:D:TEST

      20. 3.在Servlet中取得路径:

      21. (1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。

      22. 结果:E:TomcatwebappsTEST

      23. (2)得到IE地址栏地址:request.getRequestURL()

      24. 结果:http://localhost:8080/TEST/test

      25. (3)得到相对地址:request.getRequestURI()

      26. 结果:/TEST/test

      struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题,

      最好不要设置,使用struts默认

      需要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)

      public static String getRootPath() {
      String classPath = Tools.class.getClassLoader().getResource("/").getPath();
      String rootPath = "";
      //windows下
      if("\".equals(File.separator)){
      rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
      rootPath = rootPath.replace("/", "\");
      }
      //linux下
      if("/".equals(File.separator)){
      rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
      rootPath = rootPath.replace("\", "/");
      }
      return rootPath;
      }

    • .propertites
    1. 查询一个值 
      /**
           * 根据Key 获取 Value值
           * @param key
           * @return
           */
          public static String getKey(String key){
              Properties properties=new Properties();
              String value=null;
              try {
                  InputStream is= NetProperty.class.getClassLoader().getResourceAsStream("net.properties");
                  properties.load(is);
                  value=properties.getProperty(key);
                  is.close();
                  } catch (Exception e) {
              }
              return value;
          }
    2. 添加一个值
      /**
           * 添加单个数据到 net.properties
           * @param data
           */
          public static void setPropertie(String key,String value){
              
              Properties properties=new Properties();
              try {
                  InputStream is= NetProperty.class.getClassLoader().getResourceAsStream("net.properties");
                  properties.load(is);
                  properties.setProperty(key, value);
                  File file = new File(NetProperty.class.getClassLoader().getResource("net.properties").toURI());
                  FileOutputStream os =new FileOutputStream(file);
                  properties.store(os, null);
                  os.flush();
                  os.close();
                  is.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (URISyntaxException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              
          }
  • 相关阅读:
    2014年广州区域赛k题解
    2014年广州区域赛e题解
    2014年广州区域赛i题解
    最大化平均值问题
    codeforces 976e 题解
    maven
    机器学习入门
    拟合
    插值
    熵权法
  • 原文地址:https://www.cnblogs.com/Mockingjays/p/6145697.html
Copyright © 2011-2022 走看看