获取文件路径

来源:互联网 发布:scala编程思想中文版 编辑:程序博客网 时间:2024/05/22 02:43

(1) 读取文件,和获取文件全路径

      //首先读取到文件

      InputStream inputStream=this.getServletContext().getResourceAsStream("dbinfo.properties");

 

      //创建Properties

      Properties pp=newProperties();

      pp.load(inputStream);

     

      out.println("name="+pp.getProperty("username"));

 

*如果文件放在src目录下;则使用类加载器

//如果文件放在src目录下,我们应该使用类加载器来读取

      InputStream is=Servlet5.class.getClassLoader().getResourceAsStream("dbinfo.properties")

  

 

//获取文件全路径

//如果读取到一个文件的全路径

      String path=this.getServletContext().getRealPath("/imgs/Sunset.jpg");

      out.println("paht = "+path);

原创粉丝点击