分享

java获取文件路径的不同方式与不同结果

 以怪力乱神 2018-12-09
package cn.loubth.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@WebServlet(name = "ServletTest")
public class ServletTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//-------------------------------第一类--------------------------------------------------------
String path1=this.getClass().getResource("").getPath();
//结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/cn/loubth/web/
//String path2=this.getClass().getResource(".").getPath();
//结果: 报错,空指针异常
String path3=this.getClass().getResource("/").getPath();
//结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/
String path4=this.getClass().getResource("\\").getPath();
//结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/cn/loubth/web/

//-------------------------------第二类--------------------------------------------------------

String path5=this.getClass().getClassLoader().getResource("").getPath();
//结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/
String path6=this.getClass().getClassLoader().getResource(".").getPath();
//结果: /C:/apache-tomcat-7.0.77-windows-x64/apache-tomcat-7.0.77/lib/
String path7=this.getClass().getClassLoader().getResource("/").getPath();
//结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/
String path8=this.getClass().getClassLoader().getResource("\\").getPath();
//结果: /C:/Users/Administrator/IdeaProjects/FirstWebProject/out/artifacts/FirstWebProject_war_exploded/WEB-INF/classes/

//-------------------------------第三类--------------------------------------------------------

String path9=new File("").getAbsolutePath();
//结果: C:\apache-tomcat-7.0.77-windows-x64\apache-tomcat-7.0.77\bin
String path10=new File(".").getAbsolutePath();
//结果: C:\apache-tomcat-7.0.77-windows-x64\apache-tomcat-7.0.77\bin\.
String path11=new File("/").getAbsolutePath();
//结果: C:\
String path12=new File("\\").getAbsolutePath();
//结果: C:\

//-------------------------------输出结果--------------------------------------------------------

System.out.println(path1);
//System.out.println(path2); //因为报错所以就不输出了
System.out.println(path3);
System.out.println(path4);
System.out.println(path5);
System.out.println(path6);
System.out.println(path7);
System.out.println(path8);
System.out.println(path9);
System.out.println(path10);
System.out.println(path11);
System.out.println(path12);

}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多