【Java】关于文件路径问题总结

虽然文件路径在项目中不是经常的用到,用到的时候却不能灵活的使用,故在此总结。

绝对路径和相对路径

绝对路径:绝对路径是指文件在硬盘上真正存在的路径。
相对路径:相对于当前用户目录的相对路径

获取当前路径的方法

  • System.getProperty(“user.dir”)函数获取当前路径
   //获取当前用户的路径
    String path = System.getProperty("user.dir");
    System.out.println(path);

Java工程中文件路径问题

  • 获取resources目录下的文件
/**
   * 获取resources目录下的文件
   */
  @Test
  public void getResource() throws IOException {

    InputStreamReader inputStreamReader =
        new InputStreamReader(ReadAFile.class.getResourceAsStream("/1000.txt"));
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
      System.out.println(line);
    }
  }

JavaWEB中的文件路径问题

使用ServletContext方法

以加载各个文件目录小的配置文件为例。

【Java】关于文件路径问题总结_第1张图片

@WebServlet(name = "filepath2", urlPatterns = "/filepath2")
public class FilePath2Servlet extends HttpServlet {


  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    //防止出现乱码
    resp.setHeader("content-type", "text/html;charset=UTF-8");
    PrintWriter out = resp.getWriter();
    String claString = loadClaProperties("cla.properties", "VALUE");
    String rootString = loadRootProperties("webapp.properties", "VALUE");
    String info = loadINFProperties("webinfo.properties", "VALUE");
    String resString = loadResourcesProperties("res.properties", "VALUE");

    out.println(claString);
    out.println("
"
); out.println(rootString); out.println("
"
); out.println(info); out.println("
"
); out.println(resString); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } /** * 读取/WEB-INF/classes/的配置文件 */ private String loadClaProperties(String name, String key) throws IOException { String path = "/WEB-INF/classes/" + name; InputStreamReader inputStreamReader = new InputStreamReader(this.getServletContext().getResourceAsStream(path),"UTF-8"); Properties properties = new Properties(); properties.load(inputStreamReader); String value = (String) properties.get(key); return value; } /** * 读取resources的配置文件 */ private String loadResourcesProperties(String name, String key) throws IOException { String path = "/WEB-INF/classes/" + name; InputStreamReader inputStreamReader = new InputStreamReader(this.getServletContext().getResourceAsStream(path),"UTF-8"); Properties properties = new Properties(); properties.load(inputStreamReader); String value = (String) properties.get(key); return value; } /** * 加载根目录下的配置文件 */ private String loadRootProperties(String name, String key) throws IOException { String path = "/" + name; InputStreamReader inputStreamReader = new InputStreamReader(this.getServletContext().getResourceAsStream(path),"UTF-8"); Properties properties = new Properties(); properties.load(inputStreamReader); String value = (String) properties.get(key); return value; } /** * 加载/WEB-INF/中的配置文件 */ private String loadINFProperties(String name, String key) throws IOException { String path = "/WEB-INF/" + name; InputStreamReader inputStreamReader = new InputStreamReader(this.getServletContext().getResourceAsStream(path),"UTF-8"); Properties properties = new Properties(); properties.load(inputStreamReader); String value = (String) properties.get(key); return value; } }

输出结果:

我是/WEB-INF/classes/下的配置文件 
我是根目录(webapp)下的配置文件 
我是/WEB-INF/下的配置文件 
我是resources下的配置文件

你可能感兴趣的:(Java)