eclipse查找java文件路径_eclipse中java项目获取配置文件路径的问题

文件路径问题处理方法:

1把配置文件放到src folder

src folder 下的文件可以通过

MainTest.class.getClassLoader().getResource("decompress_shutdown.bat")方法获取到。

2、通过../处理路径

package com.hob.mvcapp.test;

import java.io.IOException;

import java.io.InputStream;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.Properties;

/**

* @author Administrator 根据文件名称获取项目中绝对路径

*/

public class ClassLoaderUtil {

public static ClassLoader getClassLoader() {

return ClassLoaderUtil.class.getClassLoader();

}

public static InputStream getStream(String relativePath)

throws MalformedURLException, IOException {

if (!relativePath.contains("../")) {

return getClassLoader().getResourceAsStream(relativePath);

} else {

return ClassLoaderUtil.getStreamByExtendResource(relativePath);

}

}

public static InputStream getStream(URL url) throws IOException {

if (url != null) {

return url.openStream();

} else {

return null;

}

}

public static InputStream getStreamByExtendResource(String relativePath)

throws MalformedURLException, IOException {

return ClassLoaderUtil.getStream(ClassLoaderUtil

.getExtendResource(relativePath));

}

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;

}

public static String getAbsolutePathOfClassLoaderClassPath() {

System.out.println(ClassLoaderUtil.getClassLoader().getResource("")

.toString());

return ClassLoaderUtil.getClassLoader().getResource("").toString();

}

/**

* @param relativePath

* @return 返回值是文件的绝对地址URL

* @throws MalformedURLException

*/

public static URL getExtendResource(String relativePath)

throws MalformedURLException {

System.out.println("传入的相对路径:" + 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);

}

System.out.println(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;

System.out.println("绝对路径:" + resourceAbsolutePath);

URL resourceAbsoluteURL = new URL(resourceAbsolutePath);

return resourceAbsoluteURL;

}

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;

}

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;

}

public static URL getResource(String resource) {

System.out.println("传入的相对于classpath的路径:" + resource);

return ClassLoaderUtil.getClassLoader().getResource(resource);

}

/**

* @param args

* @throws MalformedURLException

* 方法说明:根据文件获取文件的绝对路径

*/

public static void main(String[] args) throws MalformedURLException {

// ClassLoaderUtil.getExtendResource("../spring/dao.xml");

// ClassLoaderUtil.getExtendResource("../../../src/log4j.properties");

String url = ClassLoaderUtil.getExtendResource("../test/test.bat").toString();

System.out.println(url);

// System.out.println(ClassLoaderUtil.getClassLoader().getResource("../test/test.bat"));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值