获取资源相对路径


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());
}
}
在mac OS下安装SQL Developer 最近一直在折腾Oracle数据库,但由于电脑是mac系统,plsql developer用不了,远程管理一直不是很方便。后来听说Navicat不错,就也装了个试试看,发现界面看起来确实不错。今天心血来潮,想试试oracle自己开发的client。用惯plsql developer的人也可以来试试看,毕竟是自己人开发的,兼容性应该会不错吧。下面就把安装过程分享给新手或者需要帮助的朋友。 1. 阅读详情

相关推荐

unity转微信小游戏:UI大小设置

unity转微信小游戏:UI大小设置

会潜水的小火龙的博客 3148

java获取resources路径的方法

我们可以使用 java. util. exec ()方法,但是这个方法有一个致命的缺陷,那就是它只能获取一个字符串,但是如果你想要获取更多的字符串,比如你想要获取一行或者一段文本的话,那么你就必须要在上面输入更多的字符串了。(3)打开运行命令行,输入 resource. exec (),如果你的程序还没有执行完,那么我们在运行命令行中输入 resource. exec (),它就会去执行 resource对象的内容。通过上面两种方法的对比,我们可以看到,第二种方法是要比第一种方法简单的多了。

花生日记 7593

猿创征文|SfM(Structure from Motion)学习之路

SfM全称Structure from Motion,译为运动恢复结构,是三维重建pipeline的一部分,又称稀疏重建,在摄影测量领域则称为空三(空中三角测量)。SfM的任务是,给定一系列具有一定重叠度的图像,去同时估计出拍摄每张图像时相机的的位姿(位置t和姿态R)和被拍摄物体或场景的稀疏点云。...............

zeeq的博客 1万+

Java读取resources下的文件及资源路径

在Java开发中,经常需要读取项目中resources目录下的文件或获取资源路径。本文将介绍如何在Java中读取resources下的文件,并提供相关实例来说明。

算命de博客 2万+

各种读取resources目录下文件的方法

我们写使用java写web项目时,有时需要将某些文件存放到resources目录下,之后我们需要在程序中去获取文件。如果是文件路径的话getFile和getPath效果是一样的,如果是URL路径的话getFile是带有参数的路径。这个命令去获取resource的路径,下面我打断点,可以看到resource的内容,里面有file,path。注意:有的可以在web项目中使用,有的则不可以在web项目中使用。现在主流的部署方式是将项目打成jar包部署。所以我们就要通过流的方式去获取文件。

Java后端技术栈 1300

获取文件的相对路径

以上示例中,假设当前工作目录为`C:\project`,文件`example.txt`位于`C:\project\example.txt`,则输出结果将为`example.txt`,表示文件的相对路径为`example.txt`。最后,可以使用`File`类的`getCanonicalPath()`方法将文件的绝对路径与当前工作目录的路径进行比较,并使用`substring()`方法获取文件的相对路径。1. 使用`File`类的`getAbsolutePath()`方法获取文件的绝对路径。

开始的博客 1890

2018-5-8 对象构造过程 代码块 classloader(看不懂) 绝对路径与相对路径

对象构造过程 1,加载.class文件进方法区,并进行空间分配。 2,如果有静态变量,先默认初始化,显示初始化。 3,如果有静态代码块,要执行,仅一次。 4,通过new在堆内存中开辟空间,并明确首地址。 5,对对象中的属性进行默认初始化。 6,调用对应的构造函数进行初始化。 7,构造函数内部。 7.1 调用父类构造函数super();  7.2 成员变量的显示初始化。 7.3

weixin_38967434的博客 619

使用相对路径获取资源

<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

legendmoheNote的专栏 864

【Unity】由Unity资源相对路径获取资源的AssetDatabase路径

/// <summary> /// 由Unity资源相对路径获取资源的AssetDatabase路径。 /// </summary> /// <param name="assetRelativePath">Unity资源文件相对路径。</param> /// <param name="callerFilePath">请勿传入此参数。</param> /// <returns></returns> public

Arvin的博客 3230

java 相对路径获取_在java项目中通过相对路径获取资源的方式

1.可以通过 类名.class.getResource方法获取或者getSystemResource2.可以通过当前线程 Thread.currentThread().getContextClassLoader().getResource获取public class TestDemo {public static void main(String[] args) throws FileNotFou...

weixin_39633090的博客 272

java 统一路径资源定位获取相对路径与绝对路径)

前言 Java的路径问题,非常难搞。最近的工作涉及到创建和读取文件的工作,这里我就给大家彻底得解决Java路径问题。 我编写了一个方法,比ClassLoader.getResource(String 相对路径)方法的能力更强。它可以接受“../”这样的参数,允许我们用相对路径来定位clas...

chuanyuan6187的博客 1804

springboot获取相对路径文件夹下静态资源的方法

  今日遇到一个问题:springboot需要获取到一个自定义名称文件夹下的静态资源(图片等),并且文件夹的路径不在classPath下面,而是一个相对路径。   一开始使用修改配置文件的方式: # 配置静态资源访问前缀 spring.mvc.static-path-pattern=*/** # 配置静态资源路径,默认配置失效 spring.resources.static-location...

周呆呆的备忘中心 4万+

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

半亩方糖 574

在java项目中通过相对路径获取资源的方式

1.可以通过 类名.class.getResource方法获取或者getSystemResource   2.可以通过当前线程 Thread.currentThread().getContextClassLoader().getResource获取  public class TestDemo { public static void main(String[] args) throws F

zhengyangzkr的博客 1761

相对路径获取资源

[code="java"]方法1  URL url = this.getClass().getResource(imageName); 定位类路径下的资源[/code] [code="java"]方法2  URLClassLoader loader = (URLClassLoader)this.getClass().getClassLoader();    ...

hailang1130的博客 233

Servlet_GetRealPath 通过资源相对路径获取所处服务器下全路径

Servlet_GetRealPath 通过资源相对路径获取所处服务器下全路径 //获取文件全路径的Servlet类 public class getRealPathServlet extends HttpServlet { //Servlet doGet请求 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IO..

MUTONG的博客 171

ClassLoader详解

ClassLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回这个类的class对象。 1.1 几个相关概念ClassLoader负责载入系统的所有Resources(Class,文件,来自网络的字节流等),通过ClassLoader从而将资源载入JVM   每个class都有一个reference,指向自己的Cl

953

命令行下面编译运行eclipse中编写的带有包名的java文件

在eclipse下写的java文件都会带有包名,有时候想要到命令行下面执行eclipse中编辑好的java文件时。 由于带有包名用一般的编译,运行方法会报错: 错误: 找不到或无法加载主类 t20170723.FileTest 这时候要带包编译运行: 编译: javac -d . 类名.java 运行:java 包名.类名 参数1 参数2 例: 编译: javac -

小蓝的博客 1074

java获取resource下的文件路径

String path = xxx.class.getClassLoader().getResource("targetFile.txt").getPath(); #java获取文件目录 ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ ├── com │ │ │ │ ├── alipay │ │ ...

Captain249的博客 3万+

九种方式,教你读取 resources 目录下的文件路径

点击关注公众号,实用技术文章及时了解前情提要本文中提供了九种方式获取resources目录下文件的方式。其中打印文件的方法如下:/** *根据文件路径读取文件内容 * *@paramfileInPath *@throwsIOException */ publicstaticvoidgetFileContent(ObjectfileInPath)...

Java知音 2996

SpringBoot获取项目文件的绝对路径和相对路径

SpringBoot获取项目文件的绝对路径和相对路径

刘大猫 2万+

java文件路径操作详细

url:http://blog.163.com/zzwiaiao@126/blog/static/1857853200922312042289/ java文件路径操作详细   Java的路径问题,非常难搞。最近的工作涉及到创建和读取文件的工作,这里我就给大家彻底得解决Java路径问题。   我编写了一个方法,比ClassLoader.getResource(Stri

liufeng520的专栏 886

MTBF指标和计算方法.pdf

MTBF指标和计算方法.pdf

基于单片机protues仿真的有毒气体检测仪系统设计(仿真图、源代码)

基于单片机protues仿真的有毒气体检测仪系统设计(仿真图、源代码)该系统为单片机protues仿真的有毒气体检测仪系统,实现多种有毒气体检测;功能:1、使用51单片机为核心控制;2、可调电阻模拟甲醛有毒气体,甲醛的安全浓度是小于0.08mg/m3;3、可调电阻模拟苯有毒气体,苯的安全浓度是小于0.1mg/m3;4、可调电阻模拟一氧化碳有毒气体,一氧化碳的安全浓度是小于30PPM;5、有毒气体超出浓度范围,声光告警;6、LCD1602显示有毒气体浓度;7、光照强度检测;

上一篇: 替换html 中的图片
下一篇: oracle 安装 出现问题
dingh69
博客等级 码龄19年 4粉丝 34原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值