JAVA获取当前项目和文件所在路径的实例代码
时间:2021-08-17 07:34:07|栏目:JAVA代码|点击: 次
直接上代码:
//当前项目下路径
File file = new File("");
String filePath = file.getCanonicalPath();
System.out.println(filePath);
//当前项目下xml文件夹
File file1 = new File("");
String filePath1 = file1.getCanonicalPath()+File.separator+"xml\\";
System.out.println(filePath1);
//获取类加载的根路径
File file3 = new File(this.getClass().getResource("/").getPath());
System.out.println(file3);
//获取当前类的所在工程路径
File file4 = new File(this.getClass().getResource("").getPath());
System.out.println(file4);
//获取所有的类路径 包括jar包的路径
System.out.println(System.getProperty("java.class.path"));
输出结果:
1.当前项目下路径
E:\Work\example
2.当前项目下xml文件夹
E:\Work\example\xml
3.获取类加载的根路径
E:\Work\example\out\production\classes
4.获取当前类的所在工程路径
E:\Work\example\out\production\classes\com\demo
5.获取所有的类路径 包括jar包的路径D:\Java\jdk1.8.0_65\jre\lib\charsets.jar;D:\Java\jdk1.8.0_65\jre\lib\deploy.jar;
知识点补充:
Java获取当前项目文件路径
在一些时候需要用到项目的模板或其他文件,这时候可能需要组合得到文件路径。
String projectPath = System.getProperty(“user.dir”); System.out.println(“projectPath==” + projectPath);


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




