import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*@author沈东良shendl_s@hotmail.com
*Nov29,2006 10:34:34AM
*用来加载类,classpath下的资源文件,属性文件等。
*getExtendResource(StringrelativePath)方法,
*可以使用../符号来加载classpath外部的资源。
*/
public class ClassLoaderUtil
{
private static Log log = LogFactory.getLog(ClassLoaderUtil.class);
/**
*Thread.currentThread().getContextClassLoader().getResource("")
*/
/**
*加载Java类。 使用全限定类名
*@paramclassName
*@return
*/
public static Class loadClass(String className)
{
try
{
return getClassLoader().loadClass(className);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException("class not found '" + className + "'", e);
}
}
/**
*得到类加载器
*@return
*/
public static ClassLoader getClassLoader()
{
return ClassLoaderUtil.class.getClassLoader();
}
/**
*提供相对于classpath的资源路径,返回文件的输入流
*@paramrelativePath必须传递资源的相对路径。
*@是相对于classpath的路径。
*@如果需要查找classpath外部的资源,需要使用../来查找
*@return 文件输入流
*@throw sIOException
*@throw sMalformedURLException
*/
public static InputStream getStream(String relativePath)
throws MalformedURLException, IOException
{
if (!relativePath.contains("../"))
{
return getClassLoader().getResourceAsStream(relativePath);
}
else
{
return ClassLoaderUtil.getStreamByExtendResource(relativePath);
}
}
/**
*
*@paramurl
*@return
*@throw sIOException
*/
public static InputStream getStream(URL url) throws IOException
{
if (url != null)
{
return url.openStream();
}
else
{
return null;
}
}
/**
*
*@paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。
*@如果需要查找classpath外部的资源,需要使用../来查找
*@return
*@throw sMalformedURLException
*@throw sIOException
*/
public static InputStream getStreamByExtendResource(String relativePath)
throws MalformedURLException, IOException
{
return ClassLoaderUtil.getStream(ClassLoaderUtil
.getExtendResource(relativePath));
}
/**
*提供相对于classpath的资源路径,返回属性对象,它是一个散列表
*@paramresource
*@return
*/
public static Properties getProperties(String resource)
{
Properties properties = new Properties();
try
{
properties.load(getStream(resource));
}
catch (IOException e)
{
throw new RuntimeException("couldn't load properties file'"
+ resource + "'", e);
}
return properties;
}
/**
*得到本Class所在的ClassLoader的Classpat的绝对路径。
*URL形式的
*@return
*/
public static String getAbsolutePathOfClassLoaderClassPath()
{
ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource(
"").toString());
return ClassLoaderUtil.getClassLoader().getResource("").toString();
}
/**
*
*@paramrelativePath 必须传递资源的相对路径。是相对于classpath的路径。
*@如果需要查找classpath外部的资源,需要使用../来查找
*@return资源的绝对URL
*@throw sMalformedURLException
*/
public static URL getExtendResource(String relativePath)
throws MalformedURLException
{
ClassLoaderUtil.log.info("传入的相对路径:" + relativePath);
//ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../"))) ;
if (!relativePath.contains("../"))
{
return ClassLoaderUtil.getResource(relativePath);
}
String classPathAbsolutePath = ClassLoaderUtil
.getAbsolutePathOfClassLoaderClassPath();
if (relativePath.substring(0, 1).equals("/"))
{
relativePath = relativePath.substring(1);
}
ClassLoaderUtil.log.info(Integer.valueOf(relativePath
.lastIndexOf("../")));
String wildcardString = relativePath.substring(0, relativePath
.lastIndexOf("../") + 3);
relativePath = relativePath
.substring(relativePath.lastIndexOf("../") + 3);
int containSum = ClassLoaderUtil.containSum(wildcardString, "../");
classPathAbsolutePath = ClassLoaderUtil.cutLastString(
classPathAbsolutePath, "/", containSum);
String resourceAbsolutePath = classPathAbsolutePath + relativePath;
ClassLoaderUtil.log.info("绝对路径:" + resourceAbsolutePath);
URL resourceAbsoluteURL = new URL(resourceAbsolutePath);
return resourceAbsoluteURL;
}
/**
*
*@paramsource
*@paramdest
*@return
*/
private static int containSum(String source, String dest)
{
int containSum = 0;
int destLength = dest.length();
while (source.contains(dest))
{
containSum = containSum + 1;
source = source.substring(destLength);
}
return containSum;
}
/**
*
*@paramsource
*@paramdest
*@paramnum
*@return
*/
private static String cutLastString(String source, String dest, int num)
{
// String cutSource=null;
for (int i = 0; i < num; i++)
{
source = source.substring(0, source.lastIndexOf(dest, source
.length() - 2) + 1);
}
return source;
}
/**
*
*@paramresource
*@return
*/
public static URL getResource(String resource)
{
ClassLoaderUtil.log.info("传入的相对于classpath的路径:" + resource);
return ClassLoaderUtil.getClassLoader().getResource(resource);
}
/**
*@paramargs
*@throw sMalformedURLException
*/
public static void main(String[] args) throws MalformedURLException
{
//ClassLoaderUtil.getExtendResource("../spring/dao.xml");
//ClassLoaderUtil.getExtendResource("../../../src/log4j.properties");
ClassLoaderUtil.getExtendResource("log4j.properties");
System.out.println(ClassLoaderUtil.getClassLoader().getResource(
"log4j.properties").toString());
}
}
相关推荐
unity转微信小游戏:UI大小设置
unity转微信小游戏:UI大小设置
java获取resources路径的方法
我们可以使用 java. util. exec ()方法,但是这个方法有一个致命的缺陷,那就是它只能获取一个字符串,但是如果你想要获取更多的字符串,比如你想要获取一行或者一段文本的话,那么你就必须要在上面输入更多的字符串了。(3)打开运行命令行,输入 resource. exec (),如果你的程序还没有执行完,那么我们在运行命令行中输入 resource. exec (),它就会去执行 resource对象的内容。通过上面两种方法的对比,我们可以看到,第二种方法是要比第一种方法简单的多了。
猿创征文|SfM(Structure from Motion)学习之路
SfM全称Structure from Motion,译为运动恢复结构,是三维重建pipeline的一部分,又称稀疏重建,在摄影测量领域则称为空三(空中三角测量)。SfM的任务是,给定一系列具有一定重叠度的图像,去同时估计出拍摄每张图像时相机的的位姿(位置t和姿态R)和被拍摄物体或场景的稀疏点云。...............
Java读取resources下的文件及资源路径
在Java开发中,经常需要读取项目中resources目录下的文件或获取资源路径。本文将介绍如何在Java中读取resources下的文件,并提供相关实例来说明。
各种读取resources目录下文件的方法
我们写使用java写web项目时,有时需要将某些文件存放到resources目录下,之后我们需要在程序中去获取文件。如果是文件路径的话getFile和getPath效果是一样的,如果是URL路径的话getFile是带有参数的路径。这个命令去获取resource的路径,下面我打断点,可以看到resource的内容,里面有file,path。注意:有的可以在web项目中使用,有的则不可以在web项目中使用。现在主流的部署方式是将项目打成jar包部署。所以我们就要通过流的方式去获取文件。
获取文件的相对路径
以上示例中,假设当前工作目录为`C:\project`,文件`example.txt`位于`C:\project\example.txt`,则输出结果将为`example.txt`,表示文件的相对路径为`example.txt`。最后,可以使用`File`类的`getCanonicalPath()`方法将文件的绝对路径与当前工作目录的路径进行比较,并使用`substring()`方法获取文件的相对路径。1. 使用`File`类的`getAbsolutePath()`方法获取文件的绝对路径。
2018-5-8 对象构造过程 代码块 classloader(看不懂) 绝对路径与相对路径
对象构造过程 1,加载.class文件进方法区,并进行空间分配。 2,如果有静态变量,先默认初始化,显示初始化。 3,如果有静态代码块,要执行,仅一次。 4,通过new在堆内存中开辟空间,并明确首地址。 5,对对象中的属性进行默认初始化。 6,调用对应的构造函数进行初始化。 7,构造函数内部。 7.1 调用父类构造函数super(); 7.2 成员变量的显示初始化。 7.3
使用相对路径来获取资源
<br /><br />本文来自:http://blog.csdn.net/ruyanhai/archive/2007/11/07/1871663.aspx<br />◆ 一般情况下,我们都使用相对路径来获取资源,这样的灵活性比较大.<br />比如当前类为com/bbebfe/Test.class<br />而图像资源比如sample.gif应该放置在com/bbebfe/sample.gif<br />而如果这些图像资源放置在icons目录下,则应该是com/bbebfe/icons/sample.gi
【Unity】由Unity资源的相对路径获取资源的AssetDatabase路径
/// <summary> /// 由Unity资源的相对路径获取资源的AssetDatabase路径。 /// </summary> /// <param name="assetRelativePath">Unity资源文件的相对路径。</param> /// <param name="callerFilePath">请勿传入此参数。</param> /// <returns></returns> public
java 相对路径获取_在java项目中通过相对路径获取资源的方式
1.可以通过 类名.class.getResource方法获取或者getSystemResource2.可以通过当前线程 Thread.currentThread().getContextClassLoader().getResource获取public class TestDemo {public static void main(String[] args) throws FileNotFou...
java 统一路径资源定位获取(相对路径与绝对路径)
前言 Java的路径问题,非常难搞。最近的工作涉及到创建和读取文件的工作,这里我就给大家彻底得解决Java路径问题。 我编写了一个方法,比ClassLoader.getResource(String 相对路径)方法的能力更强。它可以接受“../”这样的参数,允许我们用相对路径来定位clas...
springboot获取相对路径文件夹下静态资源的方法
今日遇到一个问题:springboot需要获取到一个自定义名称文件夹下的静态资源(图片等),并且文件夹的路径不在classPath下面,而是一个相对路径。 一开始使用修改配置文件的方式: # 配置静态资源访问前缀 spring.mvc.static-path-pattern=*/** # 配置静态资源路径,默认配置失效 spring.resources.static-location...
javaweb Java路径问题, 绝对路径和相对路径,获取资源文件路径
一、绝对路径 一般不使用,例如 D:/Enviroment/hello.jsp 二、相对路径 1. 概述 1.1 不带 / (反斜杠)相对于当前资源路径 1.2 带 / 反斜杠 有3种情况(服务端,客户端,资源获取) 服务端 : http://localhost:8080/SMBMS_02_war/ (请求转发) 客户端: http://localhost:8080/ (html,重定向) 资源获取:当前类的路径 class/ 2. 更多案例参考博客 https://www.cnblogs.com
在java项目中通过相对路径获取资源的方式
1.可以通过 类名.class.getResource方法获取或者getSystemResource 2.可以通过当前线程 Thread.currentThread().getContextClassLoader().getResource获取 public class TestDemo { public static void main(String[] args) throws F
相对路径来获取资源
[code="java"]方法1 URL url = this.getClass().getResource(imageName); 定位类路径下的资源[/code] [code="java"]方法2 URLClassLoader loader = (URLClassLoader)this.getClass().getClassLoader(); ...
Servlet_GetRealPath 通过资源相对路径获取所处服务器下全路径
Servlet_GetRealPath 通过资源相对路径获取所处服务器下全路径 //获取文件全路径的Servlet类 public class getRealPathServlet extends HttpServlet { //Servlet doGet请求 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IO..
ClassLoader详解
ClassLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回这个类的class对象。 1.1 几个相关概念ClassLoader负责载入系统的所有Resources(Class,文件,来自网络的字节流等),通过ClassLoader从而将资源载入JVM 每个class都有一个reference,指向自己的Cl
命令行下面编译运行eclipse中编写的带有包名的java文件
在eclipse下写的java文件都会带有包名,有时候想要到命令行下面执行eclipse中编辑好的java文件时。 由于带有包名用一般的编译,运行方法会报错: 错误: 找不到或无法加载主类 t20170723.FileTest 这时候要带包编译运行: 编译: javac -d . 类名.java 运行:java 包名.类名 参数1 参数2 例: 编译: javac -
java获取resource下的文件路径
String path = xxx.class.getClassLoader().getResource("targetFile.txt").getPath(); #java获取文件目录 ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ ├── com │ │ │ │ ├── alipay │ │ ...
九种方式,教你读取 resources 目录下的文件路径
点击关注公众号,实用技术文章及时了解前情提要本文中提供了九种方式获取resources目录下文件的方式。其中打印文件的方法如下:/** *根据文件路径读取文件内容 * *@paramfileInPath *@throwsIOException */ publicstaticvoidgetFileContent(ObjectfileInPath)...
SpringBoot获取项目文件的绝对路径和相对路径
SpringBoot获取项目文件的绝对路径和相对路径
java文件路径操作详细
url:http://blog.163.com/zzwiaiao@126/blog/static/1857853200922312042289/ java文件路径操作详细 Java的路径问题,非常难搞。最近的工作涉及到创建和读取文件的工作,这里我就给大家彻底得解决Java路径问题。 我编写了一个方法,比ClassLoader.getResource(Stri
MTBF指标和计算方法.pdf
MTBF指标和计算方法.pdf
基于单片机protues仿真的有毒气体检测仪系统设计(仿真图、源代码)
基于单片机protues仿真的有毒气体检测仪系统设计(仿真图、源代码)该系统为单片机protues仿真的有毒气体检测仪系统,实现多种有毒气体检测;功能:1、使用51单片机为核心控制;2、可调电阻模拟甲醛有毒气体,甲醛的安全浓度是小于0.08mg/m3;3、可调电阻模拟苯有毒气体,苯的安全浓度是小于0.1mg/m3;4、可调电阻模拟一氧化碳有毒气体,一氧化碳的安全浓度是小于30PPM;5、有毒气体超出浓度范围,声光告警;6、LCD1602显示有毒气体浓度;7、光照强度检测;
3148




被折叠的 条评论
为什么被折叠?



