classpath和filepath
********************************
java中的相对路径和绝对路径
********************************
相对路径(其实就是编译后的路径)
——类路径:当前类所在的路径。不添加“/”则表示类加载的路径
@Test
public void classPath() {
// 通过获取当前类的编译后所在的路径
URL classPath = getClass().getResource("");
System.out.println("classPath = " + classPath);
}
// 输出:classPath = file:/F:/etom/pmms/target/test-classes/test/
——当前类所在的根路径,添加“/”表示类加载的根路径
@Test
public void classRootPath() {
URL classRootPath= getClass().getResource("/");
System.out.println("classRootPath= " + classRootPath);
}
// classRootPath= file:/F:/etom/pmms/target/test-classes/
——项目工程所在的根路径
@Test
public void rootPath() throws IOException {
File file = new File("");
if (!file.exists()) {
file.mkdirs();
}
String rootPath = file.getCanonicalPath();
System.out.println("rootPath = " + rootPath);
}
// rootPath = F:\etom\pmms
——当前线程的类加载器获取所在的路径
@Test
public void threadPath() {
// 通过线程的加载类所在的路径
URL threadPath = Thread.currentThread().getContextClassLoader().getResource("");
System.out.println("threadPath = " + threadPath);
}
// 输出:threadPath = file:/F:/etom/pmms/target/test-classes/
——web的相对路径
public void webPath(HttpServletRequest request) {
// 通过获取web上下文来获取web的相对路径
String webappPath = request.getServletContext().getRealPath("");
}
绝对路径
public void filePath() {
// 直接配置文件路径即可,记住:是全路径
File file = new File("F:/etom/pmms/target/test-classes/test/");
if (!file.exists()) {
file.mkdirs();
}
}
classpath和filepath的更多相关文章
- Java基础之读文件——使用通道复制文件(FileBackup)
控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...
- javac编译乱码
PersonTest.java:1: 错误: 非法字符: \65279 解决途径如下 用记事本打开java源文件,另存为ANSI格式 如果java文件包含中文字符,使用-encoding gbk格式进 ...
- 操作word
package com.gwt.flow.task; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...
- springboot-项目获取resources下文件碰到的问题(classPath下找不到文件和文件名乱码)
项目是spring-boot + spring-cloud 并使用maven 管理依赖.在springboot+maven项目下怎么读取resources下的文件实现文件下载? 怎么获取resourc ...
- JAVA获取程序(打成jar或classpath)所在目录
一.简述 JAVA获取程序(打成jar或classpath)所在目录. 二.代码 package dearcloud.utils.context; import dearcloud.utils.Str ...
- Java自定义类加载和ClassPath类加载器
1 自定义类加载器: 实现规则: 自定义类加载器,需要重写findClass,然后通过调用loadClass进行类加载(loadClass通过递归实现类的双亲委派加载) package com.dax ...
- 再次思考 classpath 环境变量 等
f:\aspectj1.8\lib\aspectjrt.jar;.;%JAVA_HOME%\lib;C:\Temp\IBM\SQLLIB\java\db2java.zip;C:\Temp\IBM\SQ ...
- web.xml 配置中classpath: 与classpath*:的区别
首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...
- CentOS下配置java环境变量classpath
一. 需要配置的环境变量1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把 jdk安装目录下的bin ...
随机推荐
- LeetCode(36)- Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- day07_Tomcat服务器与http学习笔记
============================================================ 一.Tomcat服务器(很熟悉) 1.Web开发概述 WEB,在英语中web即 ...
- Qt5中this application has requested the runtime to terminate it in an unusual way 无法运行问题的解决
在windows平台使用Qt5.8mingw版写出的程序,在Qt中运行正常,而以release的形式编译并且补充完必要的dll文件后,在其他电脑上运行出现了以下问题: 经过查阅许多资料和亲身实验,终于 ...
- Asp.NetCore+Microsoft.AspNetCore.SignalR前后端分离
1.新建WebApi 2.安装Microsoft.AspNetCore.SignalR 3.新建一个集线器和消息类 using Microsoft.AspNetCore.SignalR; using ...
- JMS(Java平台上的专业技术规范)
JMS(Java平台上的专业技术规范) 编辑 jms即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应 ...
- ScalaPB(2): 在scala中用gRPC实现微服务
gRPC是google开源提供的一个RPC软件框架,它的特点是极大简化了传统RPC的开发流程和代码量,使用户可以免除许多陷阱并聚焦于实际应用逻辑中.作为一种google的最新RPC解决方案,gRPC具 ...
- php数据导出excel
/** * 导出数据为excel表格 *@param $data 一个二维数组,结构如同从数据库查出来的数组 *@param $title excel的第一行标题,一个数组,如果为空则没有标题 *@p ...
- Spring+Redis的部署与Redis缓存使用示例
由于项目的业务需要,这两天折腾了一下Spring-redis配置,有了前面用Spring托管hibernate的经验,这次可以说是顺风顺水,大概说一下流程. ubuntu 安装 redis sudo ...
- Java中static关键字和final关键字
static: 1. 修饰变量,方法 表示静态方法,静态变量. 2. static修饰代码块 static{ } 此种形式为静态代码块,用于初始化同时被final static修饰的变量.(当然,更常 ...
- STL-Deque 源码剖析
G++ ,cygnus\cygwin-b20\include\g++\stl_deque.h 完整列表 /* * * Copyright (c) 1994 * Hewlett-Packard Comp ...